Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 11.44 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C   Managing Processes
  2. #include <csignal>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <sys/types.h>
  6.  
  7. #include <sys/ipc.h>
  8. #include <sys/shm.h>
  9. #include <sys/stat.h>
  10. #include <unistd.h>
  11. #include <cstring>
  12. #include <string>
  13. #include <sstream>
  14. #include <iostream>
  15.  
  16. using namespace std;
  17.  
  18. static void sig_usr(int);
  19. static volatile sig_atomic_t *sharedArray;
  20. static int segmentId;
  21. int number_of_children = 10;
  22.  
  23. bool check_children_finished(){
  24.   for (int i=1; i<number_of_children; i++)
  25.     if (sharedArray[i*2] == -1)
  26.       return false;
  27.   return true;
  28. }
  29.  
  30. void sig_usr (int signo)
  31. {
  32.   /*
  33.   // This is signals handler (when a signal fires this is what runs)
  34.   // we expect SIGUSR1 from the child process
  35.   if(signo == SIGUSR1)
  36.     cout << "(parent: " << (int)getpid() << ") --- caught SIGUSR1" << endl;
  37.   if (signo == SIGUSR2){
  38.     cout << "(parent: " << (int)getpid() << ") --- caught SIGUSR2" << endl;
  39.   }else
  40.     perror("unexpected signal fired");
  41.   //  return;
  42.   */
  43. }
  44.  
  45.  
  46. int main()
  47. {
  48.  
  49.   // size of the shared memory array
  50.   int arrSize = 100;
  51.   const int shareSize = sizeof(sig_atomic_t) * (arrSize);
  52.   /* Allocate shared memory segment */  
  53.   segmentId = shmget(IPC_PRIVATE, shareSize, S_IRUSR | S_IWUSR);
  54.   sharedArray = (sig_atomic_t *) shmat(segmentId, NULL, 0);
  55.  
  56.   // feel shared memory with -1
  57.   for (int i=0; i<arrSize; i++)
  58.     sharedArray[i] = -1;
  59.  
  60.   // binding SIGUSR1 & SIGUSR2 to sig_usr method
  61.   // we need to override SIGUSR2 because of group signaling
  62.   signal(SIGUSR1, sig_usr);
  63.   //  signal(SIGUSR2,sig_usr);
  64.  
  65.   fprintf(stderr, "n (parnet) myPid=%d ; segId=%dn",(int)getpid(), segmentId);
  66.   sharedArray[0] = 123;
  67.   int kids = 0; // this is the number of child processes
  68.   // we send to the child (as shell parameters) the parent pid, shared segment address , and index to the shared memory(this is where he will write his pid and in index+1 the heuristic value)
  69.   char* kidsParams[5];
  70.  
  71.   // takes care of param[0]=command to run
  72.   string exec_line = "./child";
  73.   kidsParams[0] = new char[exec_line.size()+1];
  74.   memcpy(kidsParams[0], exec_line.c_str(), exec_line.size());
  75.  
  76.   // takes care of param[1]=parent pid
  77.   kidsParams[1] = new char[100]; // = malloc(100*sizeof(char));
  78.   sprintf( kidsParams[1],"%d",(int)getpid());
  79.  
  80.   // takes care of param[2]=shared mem segment address
  81.   kidsParams[2] = new char[100];
  82.   sprintf( kidsParams[2],"%d",segmentId);
  83.  
  84.   // takes care of param[3]=the child private index in shared mem
  85.   kidsParams[3] = new char[100];
  86.  
  87.   kidsParams[4] = NULL;  // needed as end of array
  88.   int index = 0;
  89.   for(; kids<number_of_children; kids++) {
  90.     sprintf( kidsParams[3],"%d",index);
  91.     index+=2;
  92.     pid_t childpid = fork();
  93.     if(childpid==0){
  94.       execv(kidsParams[0],kidsParams);
  95.     }
  96.    }
  97.   cout << "(parent) --- just finished creating " << number_of_children << " kids" << endl;
  98.   cout << "(parent) entering to while {...} pause" << endl;
  99.   for (int i=0; i<number_of_children; i++)
  100.     cout << "[" << i << "] = " << sharedArray[i];
  101.   cout << endl;
  102.   // going to sleep --- here I want while loop with conditioning that all children finished
  103.   while ( ! check_children_finished() ) {
  104.     cout << "(parent) now will signal the group" << endl;
  105.     // killpg sends signal to the group (all the children). note that the group has the same pid as the father
  106.     killpg(getpid(),SIGUSR2);
  107.     cout << "(parent) just finished signaling the group" << endl;
  108.     pause();
  109.     for (int i=0; i<number_of_children; i++)
  110.        cout << "[" << i << "] = " << sharedArray[i];
  111.     cout << endl;
  112.   }
  113.   cout << "(parent) exited the while{...} paused" << endl;
  114.  
  115.  
  116.   // removes shared memory
  117.   //  shmdt (sharedArray);  
  118.   //  shmctl (segmentId, IPC_RMID, NULL);  
  119.   // note that all children must also shmctl (...IPC_RMID...);
  120.  
  121. }
  122.        
  123. (same includes...)
  124. using namespace std;
  125.  
  126. // declare the function proptotype (needed in signal function)
  127. static void sig_usr(int);
  128.  
  129. // handles the signal (what happens when the signal fires) --- here I want to solve the search problem
  130. void sig_usr (int signo)
  131. {
  132.   /*
  133.   if(signo == SIGUSR1){
  134.     cout << "(child: " << (int)getpid() << ") --- caught SIGUSR1" << endl;
  135.   }else if(signo == SIGUSR2){
  136.     cout << "(child: " << (int)getpid() << ") --- caught SIGUSR2" << endl;
  137.   }else
  138.     perror("eerrrr");
  139.   //  return;
  140.   */
  141.  
  142. }
  143.  
  144. int main(int argc, char** argv){
  145.   // binding the signal to the handler
  146.   signal(SIGUSR2,sig_usr);
  147.   int segmentId;  
  148.   volatile sig_atomic_t *sharedArray ;
  149.   int myIndex;
  150.   int myData =  5;
  151.   int parentPid;
  152.   // read parameters
  153.   parentPid = atoi(argv[1]);
  154.   segmentId = atoi(argv[2]);
  155.   myIndex = atoi(argv[3]);
  156.  
  157.   // declare a pointer to the shared memory
  158.   sharedArray = (sig_atomic_t *) shmat(segmentId, NULL, 0);
  159.   sharedArray[myIndex] =(int)getpid();
  160.   sharedArray[myIndex+1] = myData;
  161.   //  fprintf(stderr, "My Group Pid(child): %dn",(int)getpgrp());
  162.   cout << "(child: " << (int)getpid() << ") --- going to sleep" << endl;
  163.   pause();
  164.   cout << "(child: " << (int)getpid() << ") --- I woke up" << endl;
  165.   //calc data
  166.  
  167.   //fprintf(stderr, "My Pid(child): %dn",(int)getpid());
  168.   //fprintf(stderr, "I got %d (child)n",sharedArray[0] );
  169.  
  170.  
  171.   // this signals the father
  172.   kill(parentPid,SIGUSR1);
  173.   cout << "fired SIGUSR1"<< endl;
  174. }
  175.        
  176. (parnet) myPid=3104 ; segId=22872080
  177. (parent) --- just finished creating 10 kids
  178. (parent) entering to while {...} pause
  179. [0] = 123[1] = -1[2] = -1[3] = -1[4] = -1[5] = -1[6] = -1[7] = -1[8] = -1[9] = -1
  180. (parent) now will signal the group
  181. User defined signal 2
  182.        
  183. (parnet) myPid=3126 ; segId=22937618
  184. (child: 3129) --- going to sleep
  185. (child: 3127) --- going to sleep
  186. (parent) --- just finished creating 10 kids
  187. (parent) entering to while {...} pause
  188. [0] = 3127[1] = 5[2] = 3128[3] = 5[4] = 3129[5] = 5[6] = -1[7] = -1[8] = -1[9] = -1
  189. (parent) now will signal the group
  190. User defined signal 2
  191. (child: 3127) --- I woke up
  192. fired SIGUSR1
  193. (child: 3128) --- going to sleep
  194. (child: 3132) --- going to sleep
  195. (child: 3129) --- I woke up
  196. fired SIGUSR1
  197.        
  198. #include <csignal>
  199. #include <cstdio>
  200. #include <cstdlib>
  201. #include <sys/types.h>
  202. #include <sys/ipc.h>
  203. #include <sys/shm.h>
  204. #include <sys/stat.h>
  205. #include <unistd.h>
  206. #include <cstring>
  207. #include <string>
  208. #include <sstream>
  209. #include <iostream>
  210. #include <semaphore.h>
  211. #include <fcntl.h>
  212.  
  213. using namespace std;
  214.  
  215. char SEM_NAME[] = "lir";
  216.  
  217. static void sig_usr(int);
  218. static sig_atomic_t *sharedArray; // static volatile sig_atomic_t *sharedArray;
  219. static int segmentId;
  220.  
  221. int number_of_children = 10;
  222. int child_init_ctrl = 0;
  223. int child_finish_ctrl = 1;
  224. int number_of_children_ctrl = 2;
  225. int indexes_start_ctrl = 3;
  226. // each child has its own : PID,DATA,ADDRESS_TO_HIS_INIT_STATE
  227. int data_start = indexes_start_ctrl + number_of_children*3;
  228.  
  229. int getMyStartIndex(){
  230.   return -1;
  231. }
  232.  
  233. void sig_usr (int signo) {}
  234.  
  235. int main() {
  236.  
  237.   // create&init *new* semaphore
  238.   sem_t *mutex;
  239.   mutex = sem_open (SEM_NAME,O_CREAT,0644,1);
  240.   if(mutex == SEM_FAILED) {
  241.     perror("unable to create semaphore");
  242.     sem_unlink(SEM_NAME);
  243.     exit(-1);
  244.   }
  245.  
  246.   // size of the shared memory array
  247.   int arrSize = 100;
  248.   const int shareSize = sizeof(sig_atomic_t) * (arrSize);
  249.   /* Allocate shared memory segment */  
  250.   segmentId = shmget(IPC_PRIVATE, shareSize, S_IRUSR | S_IWUSR);
  251.   sharedArray = (sig_atomic_t *) shmat(segmentId, NULL, 0);
  252.  
  253.   // fill shared memory with -1
  254.   for (int i=0; i<arrSize; i++)
  255.     sharedArray[i] = -1;
  256.  
  257.   // binding SIGUSR1 & SIGUSR2 to sig_usr method
  258.   // we need to override SIGUSR2 because of group signaling
  259.   signal(SIGUSR1, sig_usr);
  260.   signal(SIGUSR2,sig_usr);
  261.  
  262.   sem_wait(mutex);
  263.   sharedArray[child_init_ctrl] = 0;
  264.   sharedArray[child_finish_ctrl] = 0;
  265.   sharedArray[number_of_children_ctrl] = number_of_children;
  266.   sem_post(mutex);
  267.  
  268.   // we send to the child (as shell parameters) the parent pid, shared segment address , and index to the shared memory(this is where he will write his pid and in index+1 the     heuristic value)
  269.   char* kidsParams[6];
  270.  
  271.   // takes care of param[0]=command to run
  272.   string exec_line = "./child";
  273.   kidsParams[0] = new char[exec_line.size()+1];
  274.   memcpy(kidsParams[0], exec_line.c_str(), exec_line.size());
  275.  
  276.   // takes care of param[1]=parent pid
  277.   kidsParams[1] = new char[100]; // = malloc(100*sizeof(char));
  278.   sprintf( kidsParams[1],"%d",(int)getpid());
  279.  
  280.   // takes care of param[2]=shared mem segment address
  281.   kidsParams[2] = new char[100];
  282.   sprintf( kidsParams[2],"%d",segmentId);
  283.  
  284.   // takes care of param[3]=the child private index in shared mem
  285.   kidsParams[3] = new char[100];
  286.  
  287.   // takes care of param[4]=the child's first index of start state
  288.   kidsParams[4] = new char[100]; // = malloc(100*sizeof(char));
  289.   sprintf( kidsParams[4],"%d",getMyStartIndex());
  290.  
  291.   kidsParams[5] = NULL;  // needed as end of array
  292.  
  293.   // creates the child processes (and update child private index)
  294.   int index = 3;
  295.   for(int kids=0; kids<number_of_children; kids++) {
  296.     sprintf( kidsParams[3],"%d",index);
  297.     index+=3;
  298.     pid_t childpid = fork();
  299.     if(childpid==0){
  300.       execv(kidsParams[0],kidsParams);
  301.     }
  302.   }
  303.  
  304.   while ( sharedArray[child_init_ctrl] != number_of_children ) {
  305.     // waiting on the CPU !!! or going to sleep. is there a better solution?
  306.     sleep(1);
  307.   }
  308.  
  309.   // killpg sends signal to the group (all the children). note that the group has the same     pid as the father
  310.   killpg (getpid(),SIGUSR2);
  311.  
  312.   // going to sleep --- here I want while loop with conditioning that all children finished
  313.   bool all_child_finished_flag = false;
  314.   while ( all_child_finished_flag == false ) {
  315.     sem_wait(mutex);
  316.     if (sharedArray[child_finish_ctrl] == number_of_children)
  317.       all_child_finished_flag = true;
  318.     sem_post(mutex);
  319.     if (all_child_finished_flag == false)
  320.       pause();
  321.   }  
  322.  
  323.  
  324.   // removes shared memory and delete semaphore
  325.   shmdt (sharedArray);  
  326.   shmctl (segmentId, IPC_RMID, NULL);  
  327.   // note that all children must also shmctl (...IPC_RMID...);
  328.   // removes the mutex
  329.   sem_close(mutex);
  330.   sem_unlink(SEM_NAME);
  331.  
  332. }
  333.        
  334. // declare the function proptotype (needed in signal function)
  335. static void sig_usr(int);
  336. int child_init_ctrl = 0;
  337. int child_finish_ctrl = 1;
  338. int number_of_children_ctrl = 2;
  339. int number_of_children=0;
  340.  
  341. // handles the signal (what happens when the signal fires)
  342. void sig_usr (int signo) {}
  343.  
  344. char SEM_NAME[] = "lir";
  345.  
  346. int main(int argc, char** argv) {
  347.     (same includes...)
  348.  
  349.       // create&init *existing* semaphore
  350.       sem_t *mutex;
  351.       mutex = sem_open (SEM_NAME, 0, 0644, 0);
  352.       if(mutex == SEM_FAILED) {
  353.         perror("reader:unable to execute semaphore");
  354.         sem_close(mutex);
  355.         exit(-1);
  356.       }
  357.  
  358.       // binding the signal to the handler
  359.       signal(SIGUSR2,sig_usr);
  360.  
  361.       volatile sig_atomic_t *sharedArray ;
  362.       int myData=0;
  363.       // read parameters
  364.       int parentPid = atoi(argv[1]);
  365.       int segmentId = atoi(argv[2]);
  366.       int myIndex = atoi(argv[3]);
  367.       int myStartState = atoi(argv[4]);
  368.  
  369.       // declare a pointer to the shared memory
  370.       sharedArray = (sig_atomic_t *) shmat(segmentId, NULL, 0);
  371.       sharedArray[myIndex] =(int)getpid();
  372.       sharedArray[myIndex+1] = myData;
  373.       number_of_children = sharedArray[number_of_children_ctrl];
  374.  
  375.       sem_wait(mutex);
  376.       sharedArray[child_init_ctrl]++;
  377.       sem_post(mutex);
  378.  
  379.       pause();
  380.  
  381.       // do some "calculations"
  382.       sharedArray[myIndex+1] = myIndex;
  383.       for (int i=0; i<10; i++)
  384.         sharedArray[myIndex+1] += i;
  385.  
  386.       // this signals the father
  387.       bool should_i_fire = false;
  388.       sem_wait(mutex);
  389.       sharedArray[child_finish_ctrl]++;
  390.       if (sharedArray[child_finish_ctrl] == number_of_children)
  391.         should_i_fire = true;
  392.       sem_post(mutex);
  393.       if (should_i_fire == true)
  394.         kill(parentPid,SIGUSR1);
  395.     }