Linux process programming example

Linux process programming example

Linux processes are somewhat different from threads. All variable memory is independent. Thread variables and other memory are shared similar to RTOS tasks in real-time systems. Therefore, global variables or global arrays can be used for data exchange between threads. The process must be Communication in the form of files, such as pipes, semaphores, and shared memory are all files. Next, I will give an example to talk about the process.

Header
#include <unistd.h>

Function prototype
Create process
pid_t fork()
directly exit the process
void _exit(int status)

Program example

#include<sys/types.h>
#include<unistd.h>
#include<errno.h>
//process test

void create_my_process(void)
{
pid_t pid,pid1;

pid = fork(); 
if(pid == 0)
{
	printf("process1 create ok\n");  
	while(1)
	{
		printf("process1 run\n"); 
		sleep(1);
	}
}
else if(pid < 0)
	{
		printf("process create failed\n"); 
		perror("fork");  
	}
	else
	{

	}
pid1 = fork(); 
if(pid1 == 0)
{
	printf("process create ok\n");  
	while(1)
	{
		printf("process1 run\n"); 
		sleep(2);
	}
}
else if(pid < 0)
	{
		printf("process create failed\n"); 
		perror("fork");  
	}
	else
	{
		
	}

}

int mian()
{
create_my_process();
Return 0;
}

Guess you like

Origin blog.csdn.net/u010835747/article/details/105141495