Linux system programming code summary

1. Program process thread concept_process ID number

Program: source code, instructions

Process: a running program

Threads: Threads are subordinate to processes. A process can have multiple threads. Threads directly share the resources of the process.

Task: specific thing to do

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
{
	pid_t pid;

	while(1)
	{
		printf("pid = %d\n", getpid()); //当前进程id号
		printf("ppid = %d\n", getppid());//当前进程父进程id号
		printf("Hello world\n");
		sleep(1);
	}

	return 0;
}

operation result:

 pstree -p

You can view our process tree, which is the process relationship

 

systemd(init): the parent process of all processes

2. Message queue

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/msg.h>

#define MY_TYPE  9527

int main(void)
{
	int msgid;
	pid_t pid;

	struct msgbuf
	{
		long mtype;
		char mtext[100];
		int number;
	};

	struct msgbuf buff;

	msgid = msgget(IPC_PRIVATE, 0666 | IPC_EXCL); /* 不晓得为什么必须加上0666才可以*/

	if (msgid == -1) {
	    perror("msgget");
	    return -1;
	}

	pid = fork();

	if(pid > 0)
	{
	    sleep(1);

		buff.mtype = MY_TYPE;
		printf("Please enter a string you want to send:\n");
		gets(buff.mtext);
		printf("Please enter a nubmer you want to send:\n");
		scanf("%d", &buff.number);

		msgsnd(msgid, &buff, sizeof(buff) - sizeof(buff.mtype), 0);

		waitpid(pid, NULL, 0);
	}
	else if(pid == 0)
	{
		printf("Child process is waiting for msg:\n");
		msgrcv(msgid, &buff, sizeof(buff) - sizeof(buff.mtype), MY_TYPE, 0);
		printf("Child process read from msg: %s, %d\n", buff.mtext, buff.number);
	}
	else
		perror("fork");

	return 0;
}

running result:

Use message queues to communicate between different processes

receive partial code

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/msg.h>

#define MY_TYPE  9527
#define MY_KEY	 1314

int main(void)
{
	int msgid;

	struct msgbuf
	{
		long mtype;
		char mtext[100];
		int number;
	};

	struct msgbuf buff;

	msgid = msgget(MY_KEY, IPC_CREAT|0644);

	while(1)
	{
		printf("Process is waiting for msg:\n");
		msgrcv(msgid, &buff, sizeof(buff) - sizeof(buff.mtype), MY_TYPE, 0);
		printf("Process read from msg: %s, %d\n", buff.mtext, buff.number);
	}
	msgctl(msgid, IPC_RMID, NULL);

	return 0;
}

Send partial code

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/msg.h>

#define MY_TYPE  9527
#define MY_KEY	 1314

int main(void)
{
	int msgid;

	struct msgbuf
	{
		long mtype;
		char mtext[100];
		int number;
	};

	struct msgbuf buff;

	msgid = msgget(MY_KEY, IPC_CREAT); //ftok

	buff.mtype = MY_TYPE;
	printf("Please enter a string you want to send:\n");
	gets(buff.mtext);
	printf("Please enter a nubmer you want to send:\n");
	scanf("%d", &buff.number);

	msgsnd(msgid, &buff, sizeof(buff) - sizeof(buff.mtype), 0);

	return 0;
}

 running result

 The problem I encountered was that I did not run it with root permissions at first, which caused an infinite loop when receiving messages. However, I later found that after running it with root permissions, I could receive messages normally. However, when I ran it again later, I found that after I had to send it three times, Only then did I receive the message for the fourth time. It kept happening after trying several times. I don’t know why.

Check it with the command ipcs

--------- 消息队列 -----------
键        msqid      拥有者  权限     已用字节数 消息      
0x00000522 0          root       0          0            0

It's like this. No matter how I recompile the program, it doesn't work. Then I restart the virtual machine and find that the program has root permissions. I recompiled it and it worked.

I rechecked it with the ipcs command and found that the owner and permissions have changed.

3. Shared memory

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

char msg[] = "Hello world";

int main(void)
{
	int shmid;
	pid_t pid;

	shmid = shmget(IPC_PRIVATE, 1024, IPC_CREAT);

	pid = fork();

	if(pid > 0)
	{
		char *p_addr;
		p_addr = shmat(shmid, NULL, 0);

		memset(p_addr, '\0', sizeof(msg));
		memcpy(p_addr, msg, sizeof(msg));

		shmdt(p_addr);

		waitpid(pid, NULL, 0);
	}
	else if(pid == 0)
	{
		char *c_addr;
		c_addr = shmat(shmid, NULL, 0);

		printf("Child process waits a short time: \n");
		sleep(3);
		printf("Child Process reads from shared memory: %s\n", c_addr);
		shmdt(c_addr);
	}
	else
		perror("fork");

	return 0;
}

running result:

One process writes to shared memory, and another process reads from shared memory.

write

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

char msg[] = "Hello world";

#define MY_KEY	9527

int main(void)
{
	int shmid;

	shmid = shmget(MY_KEY, 1024, IPC_CREAT);

	char *p_addr;
	p_addr = shmat(shmid, NULL, 0);

	memset(p_addr, '\0', sizeof(msg));
	memcpy(p_addr, msg, sizeof(msg));

	shmdt(p_addr);

//	shmctl(shmid, IPC_RMID, NULL);

	return 0;
}

read

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

#define MY_KEY	9527

int main(void)
{
	int shmid;

	shmid = shmget(MY_KEY, 1024, IPC_CREAT);

	char *c_addr;
	c_addr = shmat(shmid, NULL, 0);

	printf("Read from shared memory: %s\n", c_addr);
	shmdt(c_addr);

	return 0;
}

 running result:

destination write

read again

4. Mutex lock

used in threads

pthread_mutex_init initializes the mutex lock

pthread_mutex_lock(&mutex) lock

things to do

pthread_mutex_unlock(&mutex) unlock

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <semaphore.h>

void *thread1_function(void *arg);
void *thread2_function(void *arg);

pthread_mutex_t mutex;

int main(void)
{
	pthread_t pthread1, pthread2;
	int ret;

	pthread_mutex_init(&mutex, NULL);

	ret = pthread_create(&pthread1, NULL, thread1_function, NULL);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(1);
	}

	ret = pthread_create(&pthread2, NULL, thread2_function, NULL);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(1);
	}

	pthread_join(pthread1, NULL);
	pthread_join(pthread2, NULL);
	printf("The thread is over, process is over too.\n");

	return 0;
}

void *thread1_function(void *arg)
{
	int i;

	while(1)
	{
		pthread_mutex_lock(&mutex);
		for(i = 0; i < 10; i++)
		{
			printf("Hello world\n");
			sleep(1);
		}
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}
	return NULL;
}

void *thread2_function(void *arg)
{
	int i;
	sleep(1);

	while(1)
	{
		pthread_mutex_lock(&mutex);
		for(i = 0; i < 10; i++)
		{
			printf("Good moring\n");
			sleep(1);
		}
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}
	return NULL;
}

Running effect: lock first, the thread prints 10 times first, and then unlocks

Guess you like

Origin blog.csdn.net/buhuidage/article/details/130181801