Facebook
From Buff Madrill, 3 Years ago, written in C#.
This paste is a reply to ss from Antek - view diff
Embed
Download Paste or View Raw
Hits: 88
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/stat.h>
  5. #include <sys/wait.h>
  6. #include <fcntl.h>
  7. #include <semaphore.h>
  8. int main( int argc,char** argv )
  9. {
  10.  sem_t *id;
  11.  pid_t child;
  12.  int status;
  13.  id = sem_open( "szlaban",O_CREAT,S_IRUSR|S_IWUSR,0 );
  14.  switch( (int)(child=fork()) )
  15.  {
  16.  case -1: perror("...fork()..."); exit( 1 ); break;
  17. case 0: ...kod dla potomka
  18. printf("*** [%u] potomek czeka na semafor (%p)\n",(unsigned)getpid(),id);
  19. sem_wait( id );
  20. printf( "*** [%u] potomek zakończył\n",(unsigned)getpid() );
  21. exit( 0 );
  22. default: ...kod dla procesu nadrzędnego
  23. printf( "*** [%u] ustawia semafor (%p)\n",(unsigned)getpid(),id );
  24. sem_post( id );
  25. if( !wait( &status ) ){ perror( "... wait()..." ); exit( 2 ); }
  26. else
  27. {
  28. printf( "*** [%u] wygląda, że to wszystko\n",(unsigned)getpid() );
  29. sem_close( id ); sem_unlink( "szlaban" );
  30. }
  31. }
  32.  return 0;
  33. }

Replies to Re: ss rss

Title Name Language When
Re: Re: ss Colossal Bushbaby csharp 3 Years ago.