Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <time.h>
- #include <semaphore.h>
- //#include <sys/shm.h>
- #include <fcntl.h>
- //#include <errno.h>
- //#include <sys/types.h>
- #include <string.h>
- #define N 10
- typedef sem_t Semaphore;
- Semaphore *synch1;
- int print_my_info(int pid){
- printf("I am process: %d and my parent is: %d and my child is: %d\n", getpid(),getppid(),pid);
- return 1;
- }
- int main(){
- pid_t pids[N];
- srand(time(NULL));
- synch1 = sem_open("Sem1", O_CREAT | O_EXCL, 0644, 1);
- char str[80];
- strcat(str, "poo");
- for(int i=1; i<=N; i++){
- if((pids[i]=fork())<0){
- exit(1);
- }
- if((pids[i]==0)){
- char *poo = "poo";
- char t = *poo;
- //char str[80];
- strcat(poo, "foo");
- printf("%s\n",poo);
- sleep(rand() % 5);
- //sleep(rand() % 5);
- exit(0);
- }
- }
- int status;
- pid_t pid;
- int i=N;
- while (i > 0) {
- pid = wait(&status);
- printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status);
- --i; // TODO(pts): Remove pid from the pids array.
- }
- for(int i=1; i<=N; i++){
- wait(NULL);
- }
- printf("%s\n",str);
- return 0;
- }
- //###############String test#############
- /*
- #include <stdio.h>
- #include <string.h>
- int main () {
- char src[50];
- char *foo="bye";
- char *var="hello";
- //strcpy(src, "This is source");
- //strcpy(dest, "This is destination");
- strcpy(src, foo);
- strcat(src, var);
- //strcat(dest, src);
- strcat(src, var);
- printf("%s\n",src);
- return(0);
- }/*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement