Find the answer to your Linux question:
Results 1 to 2 of 2
Hi guys, as you can see I'm new in here and new in the linux programmation. I was trying to create a program where the main process create N processes ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    4

    having a hard time using signal() and kill()


    Hi guys, as you can see I'm new in here and new in the linux programmation.
    I was trying to create a program where the main process create N processes ( N=argc ) and when created, they send a signal to their father. Each time this signal is received a counter (oki++) is increased.

    My output is a mess!!! Something is missing. I tought it would be the wait() but is not.
    Code:
    int oki=0;
    
    
    
    //OK
    void ok(int sinal)
    {
    oki++;
    printf("Ok vale %d\n",oki);
    }
    
    
    //MAIN
    main(int argc, char **argv)
    {
    int pid[max],i,j,status, n, ppid;
    
    n=atoi(argv[1]);
    if (argc<=1)
    	{
    	printf("wrong sintax. \n\n");
    	exit(1);
    	}
    
    signal(SIGUSR1, ok);
    
    for(i=0;i<argc;i++)
    	{
    	pid[i]=fork();
    		if(pid[i]==0)
    		{
    		ppid=getppid();
    		printf("fathers pid  %d\n",ppid);
    		kill(ppid, SIGUSR1);
    			
    		}	
    		else
    		printf("process created number %d\n", getpid());
    		wait(&status);
    		
    	}
    
    
    printf("done\n");
    return(0);
    
    
    
    }

    This is my code.
    When I run the program with "./teste 1 2 3 " I get on the screen:

    Code:
    pubuntupubuntu:~$ ./teste 1 2 3
    process created number 6438
    fathers pid  6438
    Oki is 1
    process created number 6439
    fathers pid  6439
    process created number 6440
    Oki is 1
    fathers pid  6440
    Oki is 1
    process created number 6441
    fathers pid  6441
    done
    Oki is 1
    done
    process created number 6440
    fathers pid  6440
    Oki is 2
    done
    done
    process created number 6439
    fathers pid  6439
    Oki is 2
    process created number 6444
    fathers pid  6444
    Oki is 2
    done
    done
    process created number 6439
    fathers pid  6439
    Oki is 3
    done
    done
    process created number 6438
    fathers pid  6438
    Oki is 2
    process created number 6447
    fathers pid  6447
    process created number 6448
    Oki is 2
    fathers pid  6448
    done
    Oki is 2
    done
    process created number 6447
    fathers pid  6447
    Oki is 3
    done
    done
    process created number 6438
    fathers pid  6438
    Oki is 3
    process created number 6451
    fathers pid  6451
    done
    Oki is 3
    done
    process created number 6438
    fathers pid  6438
    Oki is 4
    done
    done
    That's far from what I was expecting. I was expecting something like:
    "
    process created number 6451
    fathers pid 6450
    oki is 1

    process created number 6452
    fathers pid 6450
    oki is 2

    .
    .
    .


    process created number 6454
    fathers pid 6450
    oki is 4
    done

    What am I doing wrong ? I think I didnt understand very well how the father/children work cause I think they are creating an orgy among them!

  2. #2
    Just Joined!
    Join Date
    Aug 2012
    Posts
    4
    Problem solved.
    some exit(0) where missing and some sleep(1) were helpful as well

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •