Posix Programming
1) How to way create a process.
2) How to way create a child process.
3) How to way create a multi-child process.
4) How to way create a thread.
5) How to way create a multithread.
6) How to way create a message Queue create.
7) How to way create a message send.
8) How to way create a message received.
Problem 1. How to create a process.
Code in the figure. 1 shows how to create a process.
Fig 1. Process create.
Description: The compiling output of the code in figure. 1 is
$ gcc –o createprocess problem0.c
./createprocess
Output:
Figure. Output of create process
The output of ps command
$ ps – afx | grep createprocess
Figure. Ps command of create process
This program we use some building functions.
getppid() and getpid() are inbuilt functions defined in unistd.h librar.
(*) fork()
System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process
- If fork() returns a negative value, the creation of a child process was unsuccessful.
- fork() returns a zero to the newly created child process.
- fork() returns a positive value, the process ID of the child process, to the parent. The returned process ID is of type pid_t defined in sys/types.h. Normally, the process ID is an integer. Moreover, a process can use function getpid() to retrieve the process ID assigned to this process.
(*) getppid() :
returns the process ID of the parent of the calling process. If the calling process was created by the fork() function and the parent process still exists at the time of the getppid function call, this function returns the process ID of the parent process. Otherwise, this function returns a value of 1 which is the process id for initprocess.
pid_t getppid(void);
Return type: getppid() returns the process ID of the parent of the current process. It never throws any error therefore is always successful.
(*) getpid() :
returns the process ID of the calling process. This is often used by routines that generate unique temporary filenames. Syntax: pid_t getpid(void);
Return type: getpid() returns the process ID of the current process. It never throws any error therefore is always successful.
(*) gid) and uid()
In a Unix system, a GID (group ID) is a name that associates a system user with other users sharing something in common (perhaps a work project or a department name). It's often used for accounting purposes. A user can be a member of more than one group and thus have more than one GID. Any user using a UNIX system at a given time has both a user ID (UID) and a group ID (GID).
Problem 2. How to create a child process
Code in the figure. 2 shows how to create a child process
Figure 2. Child process code
Description: The compiling output of the code in figure 2 is
$ gcc –o childcreate problem1.c
./childcreate
Output:
Figure. Output of child process
The output of ps command $ps –afx | grep createchild
Figure. Ps command of Create child
(*) fork()
System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process
- If fork() returns a negative value, the creation of a child process was unsuccessful.
- fork() returns a zero to the newly created child process.
- fork() returns a positive value, the process ID of the child process, to the parent. The returned process ID is of type pid_t defined in sys/types.h. Normally, the process ID is an integer. Moreover, a process can use function getpid() to retrieve the process ID assigned to this process.
Problem 3. How to create multiple child process.
Code in the figure. 3 shows how to create multiple child process
Figure 3. Multi child process code
Description: The compiling output of the code in figure 3 is
$gcc –o multiprocess problem2.c
./multiprocess
Output:
Figure. Multi child process output
Figure. Ps command of multichild process
(*) fork()
System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process
- If fork() returns a negative value, the creation of a child process was unsuccessful.
- fork() returns a zero to the newly created child process.
- fork() returns a positive value, the process ID of the child process, to the parent. The returned process ID is of type pid_t defined in sys/types.h. Normally, the process ID is an integer. Moreover, a process can use function getpid() to retrieve the process ID assigned to this process.
Problem 4. How to create a thread.
Code in the figure. 4 shows how to create a thread.
Figure 4. Thread code
Description: The compiling output of the code in figure 4 is
$gcc –o thread thread.c -lpthread
./thread
Figure. Output of thread
Figure. Ps command of thread
NAME
pthread_create - create a new thread
SYNOPSIS
#include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);
NAME
pthread_join - join with a terminated thread
SYNOPSIS
#include <pthread.h> int pthread_join(pthread_t thread, void **retval); Compile and link with -pthread.
Problem 5. How to create multiple threads
Figure 5. Multiple thread code
Description: The compiling output of the code in figure 5 is
$gcc –o multithread multithread.c –lpthread
./multithread
Figure. Multi thread output
The output of ps command $ps –afx | grep multithread
Figure. Ps command of multithread
NAME
pthread_create - create a new thread
SYNOPSIS
#include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);
NAME
pthread_join - join with a terminated thread
SYNOPSIS
#include <pthread.h> int pthread_join(pthread_t thread, void **retval); Compile and link with -pthread.
Problem 6. How to create a message
Code in the figure 6 shows how to create a message
Description: the compiling output of the code in figure 6 is
$gcc –o msgcreate createmessageQueue.c
./msgcreate
Figure. Message queue
Figure. Ipcs –q command of message create.
NAME
msgget - get a System V message queue identifier
SYNOPSIS
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> int msgget(key_t key, int msgflg);
Problem 7. How to send a message
Code in the figure 7 shows how to send a message
Figure 7. Message send in queue
Description: The compiling output of the code in figure 7 is
$gcc –o msgsend messagesend.c
./msgsend
Figure. Message sent to queue
The output of ipcs –q of message sent is
Figure. Ipcs –q of sent message
NAME
msgget - get a System V message queue identifier
SYNOPSIS
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> int msgget(key_t key, int msgflg);
Problem 8. How to receive a message
Code in the figure 8 shows how to receive a message
Figure 8. Message receive
Description:
The compiling output of the code in figure 8 is
$gcc –o msgrcv messagerecv.c
./msgrcv
Output:
Figure. Message receive output
The output of ipcs –q command for this code
Figure. Ipcs –q for message receive
NAME
msgget - get a System V message queue identifier
SYNOPSIS
#include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> int msgget(key_t key, int msgflg);
Special Notes: if we remove previous message queue id then we need to write: ipcrm -q 0( 0 is a process id).
Special Notes: if we remove previous message queue id then we need to write: ipcrm -q 0( 0 is a process id).
0 Comments
If you have any doubts, Please let me know