This function is used to set the PGID (process group id) inside the session of the calling process so that we can assign or reassign the process to different process groups. To start a new process group with any particular process as a group leader, this function also assists in this aspect. This article will highlight the creation and working of the setpgid() function.
The process id is an ID that exists within the session of a calling process. It is the ID whose PGID we want to change. It can be either the caller of setpgid() or can be the children of it. The PID cannot be the session leader that is to be changed.
PGID, on the other hand, is the new id we want to assign to that particular process specified by PID. If PGID points towards the existing process group, then it must be present inside the session of the caller. The new group is also created inside the caller’s session.
int setpgid( pid_t, pgid );
The function setpgid() is present in <process.h> library field. And this is included in <unistd.h> that’s why we are using this library here. In the parameter portion of the function, both the ids (PID, PGID) are written. Both belong to the process id we want to set or the one we want to join or create.
Facts and features of Setpgid() function
As discussed earlier, Setpgid () sets the group id of a process that PID specifies to the process group id. Sometimes there exists some circumstance where all the groups of processes lie in the same session. This occurs when one process is taken from one group to another through setpgid(). In this situation, PGID specifies the existing process group and joins it.
Getpgid() is a function like setpgid(). The syntax for both the processes is the same, including function calls. Like both these functions, getpgrp() and getpgid(0) in the same manner are equivalent to one another.
Uses of Process groups
Process groups work to distribute the signals to forward and attribute the requests for the input. Those processes that possess the same process groups are foreground and may be read, whereas the other processes will be blocked with a signal if they are attempted to be read.
When the function call is successfully done, and the process is performed, the setpgid() and setpgrp() return zero when the error occurs, and the function returns -1. After that error signal is passed. Getpgid() and getpgrp() are associated with setpgid(). So getpgid() returns a process group when done successfully. On error, it gives -1. Whereas the getpgrp() function always gives the process group currently in use.
For implementing some examples, use a text editor and Linux terminal in that aspect. We will write code in the text editors and then view the result in the Linux terminal.
Firstly, we will use the same library described above in the guide, which is essential for executing the code.
In the main program, the return type is taken as an integer. It is not void because the function will return the value in the form of an integer as process ids. We use if-statement as we have to apply a check of error. The first line in the statement calls the setpgid() with the argument of another function getpid(). This function will get the id that we want to set. If the returned value is ‘-1’ then it means an error has occurred so that the process will be terminated.
This will then again call the same setpgid function to reset the default id of the process. But if the condition is true, means the returned value is 0, then both the function calls for process id getpid(), and the group process id getpgrp() is called.
Save the file with c extension and see the output go to the Linux terminal; source code will be compiled and then executed through a GCC compiler. An input file is used that is a source code, and a file is used to display the output. ‘file. c’ is the name of a file.
The code is successfully executed when we run the code, and the process id and the process group ids are obtained. Both the IDs are the same, which means that both lie in the same session. Each time you execute the code, the resultant ids will be different.
Here we will use a FORK() in the source code. As the FORK() function is used to create a new process, the child process and this child process runs concurrently with its parent process. This is another method of setting the id and the group ids of the newly created processes. First, we have to initialize a variable to check if the error exists or not. If it exists, then an error signal is passed. And in the else-if part, the fork function is called. If the process id is equal to the fork returns 0, then it means the new process is created.
All the group ids of a child will be displayed before waiting and after it. Similarly, the parent process ids will be displayed; these are obtained through the getpid() function. In the end, the setpgid() function will be called if it is not zero, then an error is passed.
Now we will see the result. All the process ids and the process group ids will be displayed. Using fork(), parent-child ids are displayed separately. First, both parent and child process id is displayed before the wait and then after it consecutively.
The article ‘C: setpgid function usage’ is an article that contains the basic description regarding setpgid() functionality and both the process id and group process id creation and usage. Some errors are the reasons for stopping the process might be the searching errors that do not find any existing id then the process id of the current process is assigned automatically. We hope this effort will assist you in your terms of work in the C programming language.