Advertisement
deadlykingdx

test.sh

Jan 7th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <time.h>
  6. #include <semaphore.h>
  7. //#include <sys/shm.h>
  8. #include <fcntl.h>
  9. //#include <errno.h>
  10. //#include <sys/types.h>
  11. #include <string.h>
  12.  
  13. #define N 10
  14.  
  15. typedef sem_t Semaphore;
  16.  
  17. Semaphore *synch1;
  18.  
  19. int print_my_info(int pid){
  20.     printf("I am process: %d and my parent is: %d and my child is: %d\n", getpid(),getppid(),pid);
  21.     return 1;
  22. }
  23.  
  24. int main(){
  25.   pid_t pids[N];
  26.   srand(time(NULL));
  27.   synch1 = sem_open("Sem1", O_CREAT | O_EXCL, 0644, 1);
  28.   char str[80];
  29.   strcat(str, "poo");
  30.   for(int i=1; i<=N; i++){
  31.  
  32.     if((pids[i]=fork())<0){
  33.       exit(1);
  34.     }
  35.    
  36.     if((pids[i]==0)){
  37.       char *poo = "poo";
  38.       char t = *poo;
  39.       //char str[80];
  40.       strcat(poo, "foo");
  41.       printf("%s\n",poo);
  42.       sleep(rand() % 5);
  43.       //sleep(rand() % 5);
  44.       exit(0);
  45.     }
  46.   }
  47.  
  48. int status;
  49. pid_t pid;
  50. int i=N;
  51. while (i > 0) {
  52.   pid = wait(&status);
  53.   printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status);
  54.   --i;  // TODO(pts): Remove pid from the pids array.
  55. }
  56.  
  57.  
  58.   for(int i=1; i<=N; i++){
  59.     wait(NULL);
  60.   }
  61.    printf("%s\n",str);
  62.   return 0;
  63. }
  64. //###############String test#############
  65. /*
  66. #include <stdio.h>
  67. #include <string.h>
  68.  
  69. int main () {
  70.    char src[50];
  71.    char *foo="bye";
  72.    char *var="hello";
  73.    //strcpy(src,  "This is source");
  74.    //strcpy(dest, "This is destination");
  75.    strcpy(src, foo);
  76.    strcat(src, var);
  77.    //strcat(dest, src);
  78.    strcat(src, var);
  79.    printf("%s\n",src);
  80.    
  81.    return(0);
  82. }/*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement