ARTICLE · 1529996
Linux系统编程代码汇总
Linux系统编程代码汇总程序:源代码,指令 进程:运行着的程序 线程:线程从属于进程,一个进程可以有多个线程,线程直接共享进程的资源 任务:具体要做的事情 运行结果: 
可以查看我们的进程树,就是进程关系 
systemd(init): 所有进程的父进程 运行效果: 
用消息队队列让不同进程之间通信 接收部分代码 发送部分代码 运行效果 

遇到的问题,一开始是没有用root 权限运行,导致接收消息的时候出现了死循环,但是后面发现用root权限运行之后,可以正常接收消息,但是后面再运行,发现我要发三次后,才能收到第四次的消息,试了几次都是这样,不清楚为什么。 用命令ipcs看了一下 是这样的,怎么重新编译程序都不行,后面重新启动了虚拟机,发现程序是root权限的。我重新编译了一下,就可以了。 
用命令ipcs重新看了一下,发现拥有者和权限那里已经变了。 
运行效果: 
一个进程写共享内存,另外一个进程读共享内存 write read 运行效果: 先write 
再read 
用在线程中 运行效果:先上锁,线程先打印10次,再解锁
一.程序进程线程概念_进程ID号
#include<sys/types.h>#include<unistd.h>#include<stdio.h>intmain(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;}

pstree -p
二.消息队列
#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 9527intmain(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);}elseperror("fork");return 0;}

#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 1314intmain(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;}
#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 1314intmain(void){int msgid;struct msgbuf{long mtype;char mtext[100];int number;};struct msgbuf buff;msgid = msgget(MY_KEY, IPC_CREAT); //ftokbuff.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;}


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


三.共享内存
#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";intmain(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);}elseperror("fork");return 0;}

#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 9527intmain(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;}
#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 9527intmain(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;}


四.互斥锁
pthread_mutex_init 初始化互斥锁
pthread_mutex_lock(&mutex) 上锁
要做的事情
pthread_mutex_unlock(&mutex) 解锁
#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;intmain(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;}















































































