Facebook
From Commodious Cat, 3 Years ago, written in C#.
This paste is a reply to Re: Re: Re: Re: Re: ss from Gracious Macaw - go back
Embed
Viewing differences between Re: Re: Re: Re: Re: ss and Re: Re: Re: Re: Re: Re: ss
#include 
#include 
#include 
#include 
#include 
int main( void )
union semun //...struktura potrzebna dla semctl()
{
union semun
 int value; struct semid_ds *stat;
 unsigned short int *array; struct seminfo *info;
};
int main( int argc,char** argv )
{
int value; struct semid_ds *stat;
unsigned short int *array; struct seminfo *info;
 } control;
 
key_t key;
 
key;
int flag,semid;
 
semid,flag;
struct sembuf sems;
 key = ftok( "/tmp",'k'+'m' ); flag = IPC_CREAT | S_IRUSR | S_IWUSR;
 semid = semget( key,1,flag);
 control.value =+1; semctl( semid,0x0,SETVAL,control );
sems.sem_num = 0; sems.sem_flg = 0x0;
switch( (int)fork() )
P={ 0,-1,0}; //...żądanie dostępu {numer,operacja,flaga}
struct sembuf V={ 0,+1,0}; //...zwolnienie zasobu {numer,operacja,flaga}
union semun control;
int i,p,k,n;
double x;
if( argc<2 ) //...sprawdźmy, czy aby wywołanie jest poprawne
{
case -1: perror( "...fork()...\t" ); printf( "%s %s\n %s\n",argv[0],"[n]","n-krotność wykonania procesu" );
exit( 1 ); break;
case 0:
sems.sem_op =-1; semop( semid,&sems,1 
);
printf( "...child...\t:%u\n",(unsigned)getpid() );
sems.sem_op =+2; semop( semid,&sems,1 );
break;
default:
sems.sem_op =-2; semop( semid,&sems,1 );
printf( "...master...\t:%u\n",(unsigned)getpid() );
sems.sem_op =+1; semop( semid,&sems,1 );
break;
}
sscanf( argv[1],"%d",&p ); //...jeżeli tak, to czytamy ilość powtórzeń
key = ftok( "/tmp", 'k'+'m' ); flag = IPC_CREAT | S_IRUSR | S_IWUSR;
semid = semget( key,1,flag); //... tworzymy tablicę semaforów (pojedynczy)
control.value = 1; //...inicjowanie semafora (binarnego) wartością 1
semctl( semid,0x0,IPC_RMID semid,0x0,SETVAL,control );
switch( fork() ) //...utworzenie procesu potomnego
{
case -1: //...obsługa błędu fork
printf( "!.!.!...fork()...!.!.!\n" ); exit( 1 ); break;
case 0: //...kod dla procesu potomnego
printf( "...[%u]...proces potomny.......start\n",(unsigned)getpid());
fflush( stdout ); break;
default://...kod dla procesu macierzystego
printf( "...[%u]...proces macierzysty...start\n",(unsigned)getpid());
fflush( stdout ); break;
}
for( i=0;i {
semop( semid,&P,1 );
//...początek sekcji krytycznej
printf(" [%u]...rozpoczyna wykonywanie sekcji krytycznej\n",
(unsigned)getpid());
n = rand();
for( k=0,x=0;k printf( " n=%d x=%f\n",n,x/(double)n ); fflush( stdout );
printf(" [%u]...kończy wykonywanie sekcji krytycznej\n",
(unsigned)getpid() );
//...koniec sekcji krytycznej
semop( semid,&V,1 );
}
semctl( semid,0x0,IPC_RMID,control ); //...usuwamy tablicę semaforów
return 0;
}

Replies to Re: Re: Re: Re: Re: Re: ss rss

Title Name Language When
Re: Re: Re: Re: Re: Re: Re: ss Sloppy Finch csharp 3 Years ago.