LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-18-2014, 11:27 AM   #1
zvivered
Member
 
Registered: Sep 2007
Posts: 294

Rep: Reputation: 23
Setting process group id with a C code


Dear Members,

The following code creates a child process and sets it process group id to the same process group id of its parent.

Then, every 1 sec, the child and the parent prints their process group id.

For some reason, on the first print, the process group id is as specified but from this point, the process group id changes to 1.

Can you help ?

Thank you in advance,
Z.V

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
	char str_id[16];
	int rc;
			
	if (fork ()==0)
	{
		//This is the child process
		rc=setpgid (getpid(),getppid());
		if (rc==-1)
			printf ("child: setpgid faild\n");
		
		strcpy (str_id,"child");
	}
	else
	{
		//This is the parent process
		rc=setpgid (getpid(),getpid());
		if (rc==-1)
			printf ("parent: setpgid faild\n");
		strcpy (str_id,"parent");
	}

	while (1)
	{
		printf ("from process:%s %d\n",str_id,getpgid());
		sleep (1);
	}
}
 
Old 10-19-2014, 10:25 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,764

Rep: Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209Reputation: 2209
getpgid() requires an argument. Since you did not supply one, it is picking up garbage from the stack. You should include "-Wall" in your C compiler arguments. Then you would see evidence that you are missing a header file.
Code:
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main()
{
.
.
.
	while (1)
	{
		printf ("from process:%s %d\n",str_id,getpgid(0));
		sleep (1);
	}
}
 
Old 10-21-2014, 02:00 PM   #3
zvivered
Member
 
Registered: Sep 2007
Posts: 294

Original Poster
Rep: Reputation: 23
Dear Member,

Thank for your help ! Now it works OK.
I can see now that both processes (parent and child) has the same process group id.

Using gdb I attached to the parent process.
I expected the child to stop also because it is in the same process group.

But only the parent stops.
Can you tell why ?

Thank you in advance,
Z.V
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to interrupt a whole process group? rainman1985_2010 Programming 5 07-16-2012 09:01 PM
Removing a process from a group earlenceferns Linux - Newbie 2 10-31-2010 01:11 PM
How to wait for the process and know if the exit code of the sub process williamhomanchun Linux - General 10 08-11-2008 02:32 PM
Is there a way to know what is the group a process is running as? Akhran Linux - Newbie 3 04-15-2007 10:35 AM
What are Session ID, Group ID, Process ID... yrraja Linux - General 4 10-26-2003 11:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration