Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ex9a.c
- *
- * Created on: Jun 17, 2018
- Authors: joshuaad N yisraelwa
- DESCRIPTION:
- The prog' has three processes:
- 1) process father that creates a shared memory
- 2) child proces a
- 3) child process b
- the two of the children has to get a random numbers in a loop
- the frist that get a prime number is the winner.
- 1st the winner blocks the other brother from accessing to the shaed memory
- 2nd has to kill his brother
- 3rd to store the prime num in the shared memory
- 4th uses the SIGUSR1 signal. once the father get the SIGUSR1 he prints
- the prime number and quit.
- races cases are impossible due to semaphoresem_t method being used.
- */
- //============================//
- // ---INCLUDE--- //
- //============================//
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/wait.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <time.h>
- #include <semaphore.h>
- #include <fcntl.h>
- //----------------------------//
- // ---GLOBALS--- //
- //----------------------------//
- int shm_id ;
- int *shm_ptr;
- sem_t* mutex;
- //============================//
- // ---PROTOTYPES--- //
- //============================//
- void runProcess();
- void runSonA(int *shm_ptr);
- void runSonB(int *shm_ptr);
- void sigHandler();
- int isPrime(int num);
- //=========================================//
- // --MAIN FUNCTION-- //
- //=========================================//
- int main()
- {
- signal(SIGUSR1,sigHandler);
- runProcess();
- return 0;
- }
- //================================================//
- // ---PROCESS--- //
- //================================================//
- void runProcess()
- {
- key_t key ;
- if ((key = ftok("/tmp", 'x')) == -1)
- {
- perror("ftok() failed") ;
- exit(EXIT_FAILURE) ;
- }
- //-------------------------------------//
- shm_id = shmget ( key, sizeof(int)*3, IPC_CREAT |IPC_EXCL | 0600 ) ;
- if (shm_id == -1)
- {
- printf("%d",key);
- perror("shmget() failed\n") ;
- exit(EXIT_FAILURE) ;
- }
- mutex=sem_open("/mymutex",O_CREAT,0644,1);
- if(mutex==SEM_FAILED)
- {
- perror("sem_open() failed\n") ;
- }
- shm_ptr = (int *) shmat ( shm_id, NULL, 0);
- if (shm_ptr == (int *)-1)
- {
- perror( "shmat failed" ) ;
- exit( EXIT_FAILURE ) ;
- }
- //-------------------------------------//
- srand(time(NULL));
- pid_t sonB;
- pid_t sonA;
- //-------------------------------------------------//
- sonA=fork();
- if(sonA)//the father iteration
- {
- sonB=fork();
- }
- if(!sonA)
- {
- shm_ptr[1]=(int)getpid();
- runSonA(shm_ptr);
- }
- if(!sonB)
- {
- shm_ptr[2]=(int)getpid();
- runSonB(shm_ptr);
- }
- wait(NULL);
- wait(NULL);
- }
- //================================================//
- // ---SON A--- //
- //================================================//
- void runSonA(int *shm_ptr)
- {
- sem_t* s;
- s =sem_open("/mymutex", 0);
- sem_post(s);
- int num=2;
- pid_t pidbrother;
- while(1)
- {
- num=rand();
- if(isPrime(num))
- {
- sem_wait(s);
- shm_ptr[0]=num;
- printf("Hey there I won, I'm son a\n");//test
- sem_close(s);
- pidbrother=(pid_t)shm_ptr[2];
- kill(pidbrother,SIGUSR1);
- sem_unlink("/mutex" );
- // kill(getpid(),0);
- exit(EXIT_SUCCESS);
- }
- }
- }
- //================================================//
- // ---SON B--- //
- //================================================//
- void runSonB(int *shm_ptr)
- {
- sem_t* s;
- s =sem_open("/mymutex", 0);
- sem_post(s);
- int num=2;
- pid_t pidbrother=(pid_t)shm_ptr[1];
- while(1)
- {
- num=rand();
- if(isPrime(num))
- {
- sem_wait(s);
- shm_ptr[0]=num;
- printf("%s","Hey there I won, I'm son b\n");//test
- sem_close(s);
- kill(pidbrother,SIGUSR1);
- sem_unlink("/mutex" );
- exit(EXIT_SUCCESS);
- }
- }
- }
- //=========================================//
- // --SIGNAL HANDLER-- //
- //=========================================//
- void sigHandler()
- {
- printf("%d is a prime\n",shm_ptr[0]);
- shmdt(shm_ptr) ;
- if (shmctl(shm_id,IPC_RMID, NULL) == -1)
- {
- perror( "shmctl failed" ) ;
- exit( EXIT_FAILURE ) ;
- }
- exit( EXIT_SUCCESS) ;
- }
- //=========================================//
- // ---IS PRIME--- //
- //=========================================//
- int isPrime(int num)
- {
- int i=0;
- int flag=1;
- for(i=2; i<=num/2; ++i)
- {
- // condition for nonprime number
- if(num%i==0)
- {
- flag=0;
- return flag;
- }
- }
- return flag;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement