Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存
Linux進程間通信--進程,信號,管道,消息隊列,信號量,共享內存
參考:《linux編程從入門到精通》,《Linux C程序設計大全》,《unix環境高級編程》
參考:C和指針學習?
說明:本文非常的長,也是為了便于查找和比較,所以放在一起了
Linux 傳統的進程間通信有很多,如各類管道、消息隊列、內存共享、信號量等等。但它們都無法介于內核態與用戶態使用,原因如表
| 通信方法 | 無法介于內核態與用戶態的原因 |
| 管道(不包括命名管道) | 局限于父子進程間的通信。 |
| 消息隊列 | 在硬、軟中斷中無法無阻塞地接收數據。 |
| 信號量 | 無法介于內核態和用戶態使用。 |
| 內存共享 | 需要信號量輔助,而信號量又無法使用。 |
| 套接字 | 在硬、軟中斷中無法無阻塞地接收數據。 |
一.進程
1.進程表
ps顯示正在運行的進程
# ps -ef
TIME 進程目前占用的cpu時間,CMD顯示啟動進程所使用的命令
#ps ax
STAT表明進程的狀態
S 睡眠,s進程是會話期首進程;R 運行;D 等待;T 停止;Z 僵尸;N 低優先級任務,nice;W 分頁;
+進程屬于前臺進程組;l 進程是多線程;<高優先級任務
#ps -l ?或#ps -al
表現良好的程序為nice程序,系統根據進程的nice值決定他的優先級
-f是長格式
2.父子進程id
pid當前進程的;
uid當前進程的實際用戶
eid當前進程的有效用戶
#include <stdio.h>
#include <unistd.h>
main()
{
? ? printf("process id=%d\n",getpid());
? ? printf("parent process id=%d\n",getppid());
? ? printf("process group id=%d\n",getpgrp());
? ? printf("calling process's real user id=%d\n",getuid());
? ? printf("calling process's real group id=%d\n",getgid());
? ? printf("calling process's effective user id=%d\n",geteuid());
? ? printf("calling process's effective group id=%d\n",getegid());
}
運行結果:
3.設置進程組id以及進程sleep
setpgid使當前進程為新進程組的組長
#include <stdio.h>
#include <unistd.h>
main()
{
? ? setpgid(0,0); ?//設置當前進程為新進程組的組長
? ? sleep(8); ? ? ? ? //休眠8秒
}
說明:setpgid(0,0)等價于setpgrp(0,0)
setpgid(0,0)第1個參數用于指定要修改的進程id。如果為0,則指當前進程。第2個參數用于指定新的進程組id。如果為0,則指當前進程。
先運行程序
#./example13_2
再查看進程
#ps alef
#ps -ao pid,pgrp,cmd|grep 13_2 ?
或者
#ps -ao pid,pgrp,cmd
4.子進程
fork為0說明是父進程
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
main()
{
? ? fprintf(stderr,"fork...\n");
? ? if(fork() == 0 )
? ? {
? ? ? ? wait();
? ? ? ? exit(0);
? ? }
? ? printf("AA");
? ? sleep(10);
? ? exit(0);
}
輸出
fork...
AA
注意:?
警告: 隱式聲明與內建函數 ‘exit’ 不兼容?
警告: 隱式聲明與內建函數 ‘sprintf’ 不兼容???
警告: 隱式聲明與內建函數 ‘printf’ 不兼容
加入這兩個頭文件就可以了!
#include <stdio.h>?
#include <stdlib.h>
#ps -ao pid,pgrp,cmd
3165就是子進程
#ps alef
5.進程會話
setsid的調用進程應該不是某個進程組的組長進程;
setsid調用成功后生成新會話,新會話id是調用進程的進程id;
新會話只包含一個進程組一個進程即調用進程,沒有控制終端。
setid主要是實現進程的后臺運行
#include <stdio.h>
#include <unistd.h>
#include <sys/param.h>
#include <stdlib.h>
main()
{
? ? int n;
? ? __pid_t nPid;
? ? __pid_t nGroupId;
? ? if((nPid = fork()) < 0)
? ? {
? ? ? ? perror("fork");
? ? ? ? exit(0);
? ? }
? ? if(nPid != 0)//父進程
? ? ? ? exit(0);
? ? nGroupId = setsid();//新會話
? ? if(nGroupId == -1)
? ? {
? ? ? ? perror("setsid");
? ? ? ? exit(0);
? ? }
? ? for(n=0;n<10;n++)
? ? ? ? sleep(3);
}
修改后的程序
#include <stdio.h>
#include <unistd.h>
#include <sys/param.h>
#include <stdlib.h>
main()
{
? ? int n;
? ? __pid_t nPid;
? ? __pid_t nGroupId;
? ? if((nPid = fork()) < 0)
? ? {
? ? ? ? printf("Error");
? ? ? ? exit(0);
? ? }
? ? if(nPid != 0)//父進程
? ? {
? ? ? ? perror("aaa");
wait(0);
? ? ? ? //exit(0);//父進程退出
? ? }
? ? else
? ? {
? ? perror("bbb");
? ? nGroupId = setsid();//新會話
? ? if(nGroupId == -1)
? ? {
? ? ? ? perror("setsid");
? ? ? ? exit(0);
? ? }
? ? perror("fff");
? ? sleep(3);
? ? perror("ggg");
? ? exit(0);
? ? }
? ? perror("kkk");
}
父進程必須調用wait等待子進程推出,如果沒有子進程退出exit,則wait進入阻塞!
6.進程的控制終端
#tty
在secureCRT中觀看其他的會輸出
/dev/pts/1等依次類推
#ps -ax
查看進程的控制終端
有列tty的就是控制終端,有值表明進程有控制終端,無則表明是后臺進程。
延伸:php的POSIX 函數以及進程測試
7.進程的狀態
可運行;
等待;
暫停;
僵尸;
進程在終止前向父進程發送SIGCLD信號,父進程調用wait等待子進程的退出!
如果,父進程沒有調用wait而子進程已經退出,那么父進程成為僵尸進程;
如果,父進程沒有等子進程退出自己已經先退出,那么子進程成為孤兒進程;
通過top命令看到
8.進程的優先級
優先級數值越低,則優先級越高!
優先級由優先級別(PR)+進程的謙讓值(NI) ?聯合確定。
PR值是由父進程繼承而來,是不可修改的。
Linux提供nice系統調用修改自身的NI值;setpriority系統調用可以修改其他進程以及進程組的NI值。
#include <unistd.h>
#include <errno.h>
#include <sys/resource.h>
#include <stdlib.h>
#include <stdio.h>
main()
{
? ? int nPr;
? ? if(nice(3) == -1)//進程的謙讓值是3,優先級降低
? ? {
? ? ? ? perror("nice");
? ? ? ? exit(0);
? ? }
? ? errno = 0;
? ? nPr = getpriority(PRIO_PROCESS,getpid());//獲取當前進程的謙讓值
? ? if(errno != 0)
? ? {
? ? ? ? perror("getpriority");
? ? ? ? exit(0);
? ? }
? ? printf("priority is %d\n",nPr);
}
輸出:
priority is 3
9.用fork創建進程
調用fork一次返回2次,分別在父進程和子進程中返回,父進程中其返回值是子進程的進程標識符,子進程中其返回值是0。
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
? ? pid_t pid;
? ? signal(SIGCLD, SIG_IGN);//信號處理,忽略SIGCLD信號,避免形成僵尸進程
? ? switch(pid=fork())//創建子進程
? ? {
? ? ? ? case -1:
? ? ? ? ? ? perror("fork");
? ? ? ? ? ? break;
? ? ? ? case 0://子進程
? ? ? ? ? ? printf("子進程:進程ID=%d\n",getpid());
? ?printf("pid=%d\n", pid);
? ? ? ? ? ? exit(0);
? ? ? ? ? ? break;
? ? ? ? default://父進程
? ? ? ? ? ? printf("父進程:進程ID=%d\n",getpid());
? ?printf("pid=%d\n", pid);
? ? ? ? ? ? sleep(5);
? ? ? ? ? ? break;
? ? ? }
}
(注意保存為UTF-8格式,因為有中文)
輸出:
10.vfork和fork之間的區別
vfork用于創建一個新進程,而該新進程的目的是exec一個新進程,vfork和fork一樣都創建一個子進程,但是它并不將父進程的地址空間完全復制到子進程中,不會復制頁表。因為子進程會立即調用exec,于是也就不會存放該地址空間。不過在子進程中調用exec或exit之前,他在父進程的空間中運行。
為什么會有vfork,因為以前的fork當它創建一個子進程時,將會創建一個新的地址空間,并且拷貝父進程的資源,而往往在子進程中會執行exec調用,這樣,前面的拷貝工作就是白費力氣了,這種情況下,聰明的人就想出了vfork,它產生的子進程剛開始暫時與父進程共享地址空間(其實就是線程的概念了),因為這時候子進程在父進程的地址空間中運行,所以子進程不能進行寫操作,并且在兒子“霸占”著老子的房子時候,要委屈老子一下了,讓他在外面歇著(阻塞),一旦兒子執行了exec或者exit后,相當于兒子買了自己的房子了,這時候就相當于分家了。
vfork和fork之間的另一個區別是: vfork保證子進程先運行,在她調用exec或exit之后父進程才可能被調度運行。如果在調用這兩個函數之前子進程依賴于父進程的進一步動作,則會導致死鎖。
由此可見,這個系統調用是用來啟動一個新的應用程序。其次,子進程在vfork()返回后直接運行在父進程的棧空間,并使用父進程的內存和數據。這意味著子進程可能破壞父進程的數據結構或棧,造成失敗。
為了避免這些問題,需要確保一旦調用vfork(),子進程就不從當前的棧框架中返回,并且如果子進程改變了父進程的數據結構就不能調用exit函數。子進程還必須避免改變全局數據結構或全局變量中的任何信息,因為這些改變都有可能使父進程不能繼續。
通常,如果應用程序不是在fork()之后立即調用exec(),就有必要在fork()被替換成vfork()之前做仔細的檢查。
用fork函數創建子進程后,子進程往往要調用一種exec函數以執行另一個程序,當進程調用一種exec函數時,該進程完全由新程序代換,而新程序則從其main函數開始執行,因為調用exec并不創建新進程,所以前后的進程id 并未改變,exec只是用另一個新程序替換了當前進程的正文,數據,堆和棧段。
11.exec
清除父進程的可執行代碼影像,用新代碼覆蓋父進程。
參考:Linux exec與重定向
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
main()
{
? ? pid_t pid;
? ? char *para[]={"ls","-a",NULL};
? ? if((pid = fork()) < 0)
? ? {
? ? ? ? perror("fork");
? ? ? ? exit(0);
? ? }
? ? if(pid == 0)
? ? {
? ? ? ? if(execl("/bin/ls","ls","-l",(char *)0) == -1)
? ? ? ? {
? ? ? ? ? ? perror("execl");
? ? ? ? ? ? exit(0);
? ? ? ? }
? ? }
? ? if((pid = fork()) < 0)
? ? {
? ? ? ? perror("fork");
? ? ? ? exit(0);
? ? }
? ? if(pid == 0)
? ? {
? ? ? ? if(execv("/bin/ls",para) == -1)
? ? ? ? {
? ? ? ? ? ? perror("execl");
? ? ? ? ? ? exit(0);
? ? ? ? }
? ? }
? ? return;
}
12.system創建進程
system系統調用是為了方便調用外部程序,執行完畢后返回調用進程。
#include <stdio.h>
#include <stdlib.h>
main()
{
? ? printf("call ls return %d\n",system("ls -l"));
}
輸出:
13.退出進程
調用exit退出進程
調用wait等待進程退出
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
void handle_sigcld(int signo)
{
? ? pid_t pid;
? ? int status;
? ? if((pid = wait(&status)) != -1)
? ? {
? ? ? ? printf("子進程%d退出\n",pid);
? ? }
? ? if(WIFEXITED(status))
? ? {
? ? ? ? printf("子進程返回%d\n",WEXITSTATUS(status));
? ? }
? ? if(WIFSIGNALED(status))
? ? {
? ? ? ? printf("子進程被信號?%d結束\n",WTERMSIG(status));
? ? }
}
main()
{
? ? pid_t pid;
? ? signal(SIGCLD,handle_sigcld);
? ? if((pid = fork()) < 0)
? ? {
? ? ? ? perror("fork");
? ? ? ? exit(0);
? ? }
? ? if(pid == 0)
? ? {
? ? ? ?exit(123);
? ? }
? ? sleep(5);
}
輸出:
二.信號
信號又稱軟終端,通知程序發生異步事件,程序執行中隨時被各種信號中斷,進程可以忽略該信號,也可以中斷當前程序轉而去處理信號,引起信號原因:
1).程序中執行錯誤碼;
2).其他進程發送來的;
3).用戶通過控制終端發送來;
4).子進程結束時向父進程發送SIGCLD;
5).定時器生產的SIGALRM;
1.信號分類
#kill -l
獲取信號列表,信號值) ?信號名
1-31是不可靠信號(可能丟失);32-64是可靠信號(操作系統保證不丟失)
信號列表參考:http://blog.csdn.net/21aspnet/article/details/7494565
信號安裝:定義進程收到信號后的處理方法
signal系統調用安裝信號
#include <stdio.h>
#include <signal.h>
void HandleSigint(int signo)//信號處理函數
{
? ? printf("receive signal %d\n",signo);
}
main()
{
? ? if(signal(SIGINT,HandleSigint) ?== SIG_ERR)//安裝信號
? ? {
? ? ? ? perror("signal");
? ? ? ? exit(0);
? ? }
? ? pause();//暫停進程等待信號
}
輸出:
按Ctrl+C
receive signal 2
sigaction系統調用(更多的控制,完全可以替代signal)
#include <stdio.h>
#include <signal.h>
void HandleSigint(int signo,siginfo_t *info,void *none)
{
? ? printf("receive signal %d,addtional data is %d\n",signo,info->si_value.sival_int);
}
main()
{
? ? struct sigaction act,oact;//信號處理函數結構
? ? memset(&act,0x00,sizeof(struct sigaction));//清空結構
? ? sigemptyset(&act.sa_mask);//清空信號處理掩碼
? ? act.sa_sigaction = HandleSigint;//定義信號處理函數
? ? act.sa_flags = SA_SIGINFO;//指定發送信號時可以附加數據
? ? if(sigaction(SIGINT,&act,&oact) == -1)//安裝
? ? {
? ? ? ? perror("sigaction");
? ? ? ? exit(0);
? ? }
? ? pause();//暫停
}
輸出:
按Ctrl+C
receive signal 2,addtional data is 12364176
2.信號處理方式3種:
1.忽略信號-大多可以忽略,只有SIGKILL和SIGSTOP除外;
2.捕捉信號-先安裝
3.默認操作
3.信號阻塞
阻塞是指系統內核暫停向進程發送指定信號,由內核對進程接收到的信號緩存,直到解除阻塞為止。
信號3種進入阻塞的情況:
1.信號處理函數執行過程中,該信號將阻塞;
2.通過sigaction信號安裝,如果設置了sa_mask阻塞信號集;
3.通過系統調用sigprocmask
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
參數:
how:用于指定信號修改的方式,可能選擇有三種
SIG_BLOCK //加入信號到進程屏蔽。
SIG_UNBLOCK //從進程屏蔽里將信號刪除。
SIG_SETMASK //將set的值設定為新的進程屏蔽。
set:為指向信號集的指針,在此專指新設的信號集,如果僅想讀取現在的屏蔽值,可將其置為NULL。
oldset:也是指向信號集的指針,在此存放原來的信號集。
#include <stdio.h>
#include <signal.h>
void handle_sigint(int signo)
{
? ? printf("receive signal %d\n",signo);
}
main()
{
? ? sigset_t mask;
? ? sigset_t omask;
? ? signal(SIGINT,handle_sigint);
? ? sigemptyset(&mask);//清空信號處理掩碼
? ? sigaddset(&mask,SIGINT);//向掩碼中增加信號
? ? sigprocmask(SIG_BLOCK,&mask,&omask);//設置掩碼,設置完成后SIGINT信號被阻塞
? ? sleep(10);
? ? sigprocmask(SIG_SETMASK,&omask,NULL);//恢復原有的信號處理掩碼
}
輸出:如果不輸入Ctrl+C則10秒后程序結束;如果期間有Ctrl+C則會10秒結束,之后輸出Creceive signal 2
注意:子進程會繼承父進程的信號掩碼
4.信號集操作
對信號集中所有信號處理
數據類型 sigset_t
清空信號集sigemptyset
信號集填充全部信號sigfillset
信號集增加信號sigaddset
信號集中刪除信號sigdelset
判斷信號集是否包含某信號的sigismember
5.未決信號
信號產生后到信號被接收進程處理前的過渡狀態,未決狀態時間很短。
sigprocmask阻塞某種信號,則向進程發送這種信號處于未決狀態。
sigpending獲取當前進程中處于未決狀態的信號
#include <stdio.h>
#include <signal.h>
void handle_sigint(int signo)
{
? ? printf("receive signal %d\n",signo);
}
main()
{
? ? sigset_t mask;
? ? sigset_t omask;
? ? sigset_t pmask;
? ? signal(SIGINT,handle_sigint);
? ? sigemptyset(&mask);
? ? sigaddset(&mask,SIGINT);
? ? sigprocmask(SIG_BLOCK,&mask,&omask);
? ? sleep(10);
? ? if(sigpending(&pmask) < 0)//獲取當前未決的信號集
? ? {
? ? ? ? perror("sigpending");
? ? ? ? exit(0);
? ? }
? ? if(sigismember(&pmask,SIGINT))//判定SIGINT是否在未決信號集中
? ? {
? ? ? ? printf("SIGINT signal is pending.\n");
? ? }
}
6.等待信號
阻塞式系統如果沒有符合條件的數據將休眠,直到數據到來,例如socket上讀取數據。有2種狀態可以中斷該操作
1.網絡上有數據,讀操作獲取數據后返回
2.當前進程接收信號,讀操作被中斷返回失敗,錯誤碼errno為EINTR
pause系統調用可以讓程序暫停執行進入休眠,等待信號到來。
#include <stdio.h>
#include <signal.h>
int nInterrupt;
void handle_sigint(int signo)
{
? ? nInterrupt = 1;
}
main()
{
? ? sigset_t mask,omask;
? ? signal(SIGINT,handle_sigint);
? ? sigemptyset(&mask);
? ? sigaddset(&mask,SIGINT);
? ? sigprocmask(SIG_BLOCK,&mask,&omask);//阻塞
? ? nInterrupt = 0;
? ? while(!nInterrupt)//循環調用sigsuspend等待信號,直到收到SIGINT,nInterrupt為1
? ? ? ? sigsuspend(&omask);//阻塞信號直到有信號到達
? ? printf("process return.\n");
}
7.信號發送
兩種方式
kill 不可附加數據
sigqueue?可附加數據
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void HandleSigint(int signo,siginfo_t *info,void *none)
{
? ? printf("receive addtional data is %d\n",info->si_value.sival_int);
? ? exit(0);
}
main()
{
? ? int pid;
? ? struct sigaction act;
? ? union sigval sigvalPara;
? ? if((pid = fork()) == 0)
? ? {
? ? ? ? memset(&act,0x00,sizeof(struct sigaction));
? ? ? ? sigemptyset(&act.sa_mask);
? ? ? ? act.sa_sigaction = HandleSigint;
? ? ? ? act.sa_flags = SA_SIGINFO;
? ? ? ? if(sigaction(SIGINT,&act,NULL) == -1)
? ? ? ? {
? ? ? ? ? ? perror("sigaction");
? ? ? ? ? ? exit(0);
? ? ? ? }
? ? ? ? pause();//暫停子進程,等待信號
? ? }
? ? else
? ? {
? ? ? ? sigvalPara.sival_int = 123;//設置附加數據為123
? ? ? ? if(sigqueue(pid,SIGINT,sigvalPara) == -1)//向子進程發送信號SIGINT,并附加數據
? ? ? ? {
? ? ? ? ? ? perror("sigqueue");
? ? ? ? ? ? exit(0);
? ? ? ? }
? ? }
}
輸出:receive addtional data is 123
8.sigalarm信號
阻塞式系統調用,為避免無限期等待,可以設置定時器信號,alarm調用
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
int timeout;
void Handle_Alarm(int signo)
{
? ? timeout = 1;
? ? printf("SIGALRM received.\n");
}
main()
{
? ? if(signal(SIGALRM,Handle_Alarm) ==SIG_ERR )//安裝SIGALRM信號
? ? {
? ? ? ? perror("signal");
? ? ? ? exit(0);
? ? }
? ? timeout = 0;//設置超時標志為0
? ? alarm(10);//啟動定時器
? ? pause();//阻塞進程,等待信號
? ? if(timeout)//如果超時
? ? {
? ? ? ? printf("Pause time out.\n");
? ? }
}
輸出:
SIGALRM received.
Pause time out.
9.sigcld信號
父進程捕獲子進程的退出信號
子進程發送SIGCLD信號進入僵尸狀態;父進程接收到該信號處理,子進程結束
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void Handle_Sigcld(int signo)
{
? ? ?int pid,status;
? ? ?pid = waitpid(-1,&status,0);
? ? ?printf("Child process %d exit with code %d\n",pid,status);
}
main()
{
? ? int i,pid;
? ? signal(SIGCLD,Handle_Sigcld);
? ? for(i=0;i<5;i++)
? ? {
? ? ? ? if((pid = fork()) == 0)//子進程
? ? ? ? {
? ? ? ? ? ? srand(getpid());//產生隨機數
? ? ? ? ? ? exit((int)(rand()/1024));//退出子進程,退出碼為上步隨機數
? ? ? ? }
? ? ? ? else
? ? ? ? {//父進程
? ? ? ? ? ? sleep(1);//休眠
? ? ? ? ? ? continue;//繼續
? ? ? ? }
? ? }
}
輸出:
三.管道
單向,一段輸入,另一端輸出,先進先出FIFO。管道也是文件。管道大小4096字節。
特點:管道滿時,寫阻塞;空時,讀阻塞。
分類:普通管道(僅父子進程間通信)位于內存,命名管道位于文件系統,沒有親緣關系管道只要知道管道名也可以通訊。
1.pipe建立管道
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
? ? int pfd[2]; //保存打開管道后的兩個文件描述符
? ? pid_t cpid;//保存進程標識符
? ? char buf;
? ? if(argc != 2)//判斷命令行參數是否符合
? ? {
? ? ? ? fprintf(stderr,"Usage: %s <string>\n",argv[0]);
? ? ? ? exit(0);
? ? }
? ? if (pipe(pfd) == -1)//建立管道
? ? {
? ? ? ? perror("pipe");
? ? ? ? exit(EXIT_FAILURE);
? ? }
? ? cpid = fork();
? ? if (cpid == -1)
? ? {
? ? ? ? perror("fork");
? ? ? ? exit(EXIT_FAILURE);
? ? }
? ? if (cpid == 0)//子進程
? ? {
? ? ? ? close(pfd[1]); ? ? ? ? ?//關閉管道寫,引用計數-1?
? ? ? ? while (read(pfd[0], &buf, 1) > 0)//從管道循環讀取數據
? ? ? ? ? ? write(STDOUT_FILENO, &buf, 1);//輸出讀到的數據
? ? ? ? write(STDOUT_FILENO, "\n", 1);//輸出從管道讀取的數據
? ? ? ? close(pfd[0]);//關閉管道讀,引用計數-1?
? ? ? ? exit(0);
? ? }
? ? else
? ? {//父進程
? ? ? ? close(pfd[0]); ? ? ? ??
? ? ? ? write(pfd[1], argv[1], strlen(argv[1]));//向管道寫入命令行參數1
? ? ? ? close(pfd[1]); ? ? ? ? ?
? ? ? ? wait(NULL); ? ? ? ? ? //等待子進程退出
? ? ? ? exit(0);
? ? }
}
說明:每調用一次fork? 都要關閉一次進程描述符
執行
#./a.out ? www
輸出
#www
2.dup
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main()
{
int pfds[2];
if ( pipe(pfds) == 0 )
{
? ? if ( fork() == 0 )//子進程
? ? {
? ? ? ? close(1);//關閉標準輸出
? ? ? ? dup2( pfds[1], 1 );//管道的寫文件描述符復制到進程的輸出
? ? ? ? close( pfds[0] );//關閉管道讀
? ? ? ? execlp( "ls", "ls","-l", NULL );//執行ls -l 輸出寫入管道
? ? }
? ? else
? ? {
? ? ? ? close(0);
? ? ? ? dup2( pfds[0], 0 );//管道的讀文件描述符復制到進程的輸入
? ? ? ? close( pfds[1] );
? ? ? ? execlp( "wc", "wc", "-l", NULL );//執行wc -l 將管道讀取數據作為wc命令的輸入
? ? ?}
}
return 0;
}
輸出:129
?
Linux execlp函數說明:相當于執行# ls -l |wc -l 統計當前目錄下文件數量;ls -l 列出當前文件詳細信息;wc -l
wc參考http://blog.csdn.net/21aspnet/article/details/7515442
linux命令集錦http://blog.csdn.net/21aspnet/article/details/1534099
linux常用命令http://linux.chinaitlab.com/special/linuxcom/
3.popen() 函數
用于創建一個管道,其內部實現為調用 fork 產生一個子進程,執行一個 shell 以運行命令來開啟一個進程,這個進程必須由 pclose() 函數關閉。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main( void )
{
? ? FILE ? *stream;//文件流
? ? char ? buf[1024];//讀寫緩沖區
? ? memset( buf, '\0', sizeof(buf) );//清空
? ? stream = popen( "wc -l", "w" );
? ? for(;;)
? ? {
? ? ? ? memset(buf,0x00,sizeof(buf));
? ? ? ? scanf("%s",buf);//接受輸入
? ? ? ? if(strcmp(buf,"k") == 0)//如果是k就退出
? ? ? ? {
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? fprintf(stream,"%s\n",buf);//寫入
? ? }
? ? pclose( stream );//關閉
? ? return 0;
}
輸出:
4.命名管道
mknod
mknod 管道名稱 p
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int mknod(const char *pathname, mode_t mode, dev_t dev);
mkfifo
mkfifo -m 權限?管道名稱
#include<sys/types.h>
#include<sys/stat.h>
int mkfifo(const char * pathname,mode_t mode);
mknod和mkfifo的區別
mknod系統調用會產生由參數path鎖指定的文件,生成文件類型和訪問權限由參數mode決定。
在很多unix的版本中有一個C庫函數mkfifo,與mknod不同的是多數情況下mkfifo不要求用戶有超級用戶的權限
利用命令創建命名管道p1.
#mkfifo -m 0644 p1
#mknod p2 p
#ll
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
main()
{
? ? if(mkfifo("p1",0644) < 0)
? ? {
? ? ? ? perror("mkfifo");
? ? ? ? exit(-1);
? ? }
? ? return;
}
5.管道讀寫
通過open打開,默認是阻塞方式打開,如果open指定O_NONBLOCK則以非阻塞打開。
O_NONBLOCK和O_NDELAY所產生的結果都是使I/O變成非擱置模式(non-blocking),在讀取不到數據或是寫入緩沖區已滿會馬上return,而不會擱置程序動作,直到有數據或寫入完成。
它們的差別在于設立O_NDELAY會使I/O函式馬上回傳0,但是又衍生出一個問題,因為讀取到檔案結尾時所回傳的也是0,這樣無法得知是哪中情況;因此,O_NONBLOCK就產生出來,它在讀取不到數據時會回傳-1,并且設置errno為EAGAIN。
不過需要注意的是,在GNU C中O_NDELAY只是為了與BSD的程序兼容,實際上是使用O_NONBLOCK作為宏定義,而且O_NONBLOCK除了在ioctl中使用,還可以在open時設定。
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
main()
{
? ? int fd;
? ?
? ? if((fd = open("p1",O_RRONLY,0)) < 0)//只讀打開管道
? // if((fd = open("p1",O_WRONLY,0)) < 0)//只寫打開管道
? ? {
? ? ? ? perror("open");
? ? ? ? exit(-1);
? ? }
? ? printf("open fifo p1 for write success!\n");
? ? close(fd);
}
四.IPC對象
查看ipc對象信息
#ipcs
查看全部ipc對象信息
#ipcs -a
查看消息隊列信息
#ipcs -q
查看共享內存信息
#ipcs -m
查看信號量信息
#ipcs -s
刪除IPC對象的ipcrm
ipcrm -[smq] ID 或者ipcrm -[SMQ] Key
-q ?-Q刪除消息隊列信息 ?例如ipcrm -q 98307
-m -M刪除共享內存信息
-s -S刪除信號量信息
ftok函數
產生一個唯一的關鍵字值
ftok原型如下:
key_t ftok( char * fname, int id )
fname就是你指定的文件名(該文件必須是存在而且可以訪問的),id是子序號,雖然為int,但是只有8個比特被使用(0-255)。
當成功執行的時候,一個key_t值將會被返回,否則 -1 被返回。
? ?在一般的UNIX實現中,是將文件的索引節點號取出,前面加上子序號得到key_t的返回值。如指定文件的索引節點號為65538,換算成16進制為 0x010002,而你指定的ID值為38,換算成16進制為0x26,則最后的key_t返回值為0x26010002。
查詢文件索引節點號的方法是: ls -i
以下為測試程序:
ftok.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#define IPCKEY 0x11
int main( void )
{
? ? int i=0;
? ? for ( i = 1; i < 256; ++ i )
? ? ? ? printf( "key = %x\n", ftok( "/tmp", i ) );
? ? return 0;
}
#ls -i ftok.c
#./a.out
五.消息隊列
消息隊列是先進先出FIFO原則
1.消息結構模板
strut msgbuf
{
long int ?mtype;//消息類型
char mtext[1];//消息內容
}
2.msgget創建消息
#include <sys/msg.h>
int msgget(key_t key, int flag);
此函數返回key指定消息的標識符
key 一般有ftok函數產生 ,該函數為key_t ftok(const char *pathname, int proj_id);
該函數把從pathname導出的信息與id低8位組合成一個整數IPC鍵, 調用時pathname必須存在,若不存在ftok調用失敗,返回-1,成功返回該整數IPC鍵值
flag 為該消息隊列的讀寫權限組合,可以與IPC_CREAT 或IPC_EXCL相與,其中創建對列時都要使用IPC_CREAT,其中IPC_CREAT|IPC_EXCL含義是若已有該隊列則返回錯誤
此函數成功時,返回非負隊列標識符;失敗時返回-1
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
main()
{
? ? key_t lKey;
? ? int nMsgId;
? ? if((lKey = ftok("/etc/profile",1)) == -1)
? ? {
? ? ? ? perror("ftok");
? ? ? ? exit(1);
? ? }
//帶參數IPC_CREAT和IPC_EXCL,如果隊列不存在則創建隊列,已存在則返回EEXIST
? ? if((nMsgId = msgget(lKey,IPC_CREAT|IPC_EXCL|0666)) == -1)
? ? {
? ? ? ? if(errno != EEXIST)//創建失敗且不是由于隊列已存在
? ? ? ? {
? ? ? ? ? ? perror("msgget");
? ? ? ? ? ? exit(2);
? ? ? ? }
? ? ? ? if((nMsgId = msgget(lKey,0)) == -1)//已存在
? ? ? ? {
? ? ? ? ? ? perror("msgget");
? ? ? ? ? ? exit(3);
? ? ? ? }
? ? }
? ? printf("MsgID=%d\n",nMsgId);
? ? return 0;
}
3.msgsnd消息發送
int msgsnd(int msqid, const void *ptr, size_t length, int flag);
此函數發送消息到指定的消息對列
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
typedef struct
{
? ? long int nType;
? ? char szText[256];
}MSG;
main()
{
? ? key_t lKey;
? ? int nMsgId;
? ? MSG msg;
? ? if((lKey = ftok("/etc/profile",1)) == -1)//生成鍵值
? ? {
? ? ? ? perror("ftok");
? ? ? ? exit(1);
? ? }
? ? if((nMsgId = msgget(lKey,IPC_CREAT|IPC_EXCL|0666)) == -1)//創建消息隊列
? ? {
? ? ? ? if(errno != EEXIST)
? ? ? ? {
? ? ? ? ? ? perror("msgget");
? ? ? ? ? ? exit(2);
? ? ? ? }
? ? ? ? if((nMsgId = msgget(lKey,0)) == -1)
? ? ? ? {
? ? ? ? ? ? perror("msgget");
? ? ? ? ? ? exit(3);
? ? ? ? }
? ? }
? ? memset(&msg,0x00,sizeof(MSG));//清空隊列
? ? msg.nType = 2;//指定消息類型為2
? ? memcpy(msg.szText,"123456",6);//指定消息內容
? ? if(msgsnd(nMsgId,(const void *)&msg,strlen(msg.szText),IPC_NOWAIT) < 0)//非阻塞發送消息
? ? {
? ? ? ? perror("msgsnd");
? ? }
? ? return 0;
}
隊列中已經有一條消息,長度6字節
4.msgrcv消息發送
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
typedef struct
{
? ? long int nType;
? ? char szText[256];
}MSG;
main()
{
? ? key_t lKey;
? ? int n,nMsgId;
? ? MSG msg;
? ? if((lKey = ftok("/etc/profile",1)) == -1)
? ? {
? ? ? ? perror("ftok");
? ? ? ? exit(1);
? ? }
? ? if((nMsgId = msgget(lKey,0)) == -1)
? ? {
? ? ? ? perror("ftok");
? ? ? ? exit(2);
? ? }
? ? memset(&msg,0x00,sizeof(MSG));
? ? if((n = msgrcv(nMsgId,(void *)&msg,sizeof(msg.szText),2L,0)) < 0)//從隊列接收消息,讀出以后就不存在了
? ? {
? ? ? ? perror("msgrcv");
? ? }
? ? else
? ? {
? ? ? ? printf("msgrcv return length=[%d] text=[%s]\n",n,msg.szText);//輸出
? ? }
? ? return 0;
}
輸出:
msgrcv return length=[6] text=[123456]5.msgctl控制消息
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
消息隊列控制函數
其中msqid為消息隊列描述符
cmd有以下三種:
IPC_RMID:刪除msgid指定的消息隊列,當前在該隊列上的任何消息都被丟棄,對于該命令,buf參數可忽略
IPC_SET:設置消息隊列msgid_ds結構體的四個成員:msg_perm.uid,msg_perm_gid,msg_perm.mode和msg_qbytes。它們的值來自由buf指向的結構體中的相應成員。
IPC_STAT:給調用者通過buf返回指定消息隊列當前對應msgid_ds結構體
函數執行成功返回0,失敗返回-1
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
typedef struct
{
? ? long int nType;
? ? char szText[256];
}MSG;
main()
{
? ? key_t lKey;
? ? int n,nMsgId;
? ? MSG msg;
? ? struct msqid_ds qds;
? ? if((lKey = ftok("/etc/profile",1)) == -1)
? ? {
? ? ? ? perror("ftok");
? ? ? ? exit(1);
? ? }
? ? if((nMsgId = msgget(lKey,0)) == -1)
? ? {
? ? ? ? perror("ftok");
? ? ? ? exit(2);
? ? }
? ? memset(&qds,0x00,sizeof(struct msqid_ds));
? ? if(msgctl(nMsgId,IPC_STAT,&qds) < 0)//獲取消息隊列屬性,獲取狀態放pds中
? ? {
? ? ? ? perror("msgctl IPC_STAT");
? ? ? ? exit(3);
? ? }
? ? printf("msg_perm.mode=%d\n",qds.msg_perm.mode);
? ? qds.msg_perm.mode &= (~0222);//去除消息隊列的寫權限
? ? if(msgctl(nMsgId,IPC_SET,&qds) < 0)//設置消息隊列權限
? ? {
? ? ? ? perror("msgctl IPC_SET");
? ? ? ? exit(4);
? ? }
? ? memset(&msg,0x00,sizeof(MSG));
? ? msg.nType = 2;
? ? memcpy(msg.szText,"12345",5);
? ? if(msgsnd(nMsgId,(void *)&msg,5,0) < 0)//發送消息
? ? {
? ? ? ? perror("msgsnd");
? ? }
? ? if(msgctl(nMsgId,IPC_RMID,NULL) < 0)//刪除消息
? ? {
? ? ? ? perror("msgctl IPC_RMID");
? ? ? ? exit(5);
? ? }
? ? return 0;
}
說明: (~0222)取反后做與實際上就是去除其他用戶的寫權限,在C語言中,八進制常用用前綴表示
六.共享內存
共享內存是分配一塊能被其他進程訪問的內存,實現是通過將內存去映射到共享它的進程的地址空間,使這些進程間的數據傳送不再涉及內核,即,進程間通信不需要通過進入內核的系統調用來實現;
共享內存與其他的進程間通信最大的優點是:數據的復制只有兩次,一次是從輸入文件到共享內存區,一次從共享內存區到輸出文件
而其他的則是需要復制4次:服務器將輸入文件讀入自己的進程空間,再從自己的進程空間寫入管道/消息隊列等;客戶進程從管道/消息隊列中讀出數據到自己的進程空間,最后輸出到客戶指定的文件中;
要使用共享內存,應該有如下步驟:
1.開辟一塊共享內存 ? ? shmget()
2.允許本進程使用共某塊共享內存 ?shmat()
3.寫入/讀出
4.禁止本進程使用這塊共享內存 ? shmdt()
5.刪除這塊共享內存 ? ? shmctl()或者命令行下ipcrm
1.shmget創建共享內存
#include <sys/shm.h>
int ? ?shmget( key_t shmkey , int shmsiz , int flag );
shmget()是用來開辟/指向一塊共享內存的函數。參數定義如下:
key_t shmkey 是這塊共享內存的標識符。如果是父子關系的進程間通信的話,這個標識符用IPC_PRIVATE來代替
int shmsiz 是這塊內存的大小.
int flag 是這塊內存的模式(mode)以及權限標識
模式可取如下值: ? ? ? 新建:IPC_CREAT
? ? ? ? ? ? ? ? ? ? ? ?使用已開辟的內存:IPC_ALLOC
? ? ? ? ? ? ? ? ? ? ? ?如果標識符以存在,則返回錯誤值:IPC_EXCL
然后將“模式” 和“權限標識”進行“或”運算,做為第三個參數
如: ? ?IPC_CREAT| IPC_EXCL | 0666
這個函數成功時返回共享內存的ID,失敗時返回-1。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
main()
{
? ? key_t lKey;
? ? int nShmId;
? ? if((lKey = ftok("/etc/profile",1)) < 0)
? ? {
? ? ? ? perror("ftok");
? ? ? ? exit(1);
? ? }
? ? if((nShmId = shmget(lKey,256,IPC_CREAT|0666)) == -1)//創建
? ? {
? ? ? ? perror("shmget");
? ? ? ? exit(2);
? ? }
? ? printf("Shmid=%d\n",nShmId);
? ? return 0;
}
2.shmat映射共享內存
shmat()是用來允許本進程訪問一塊共享內存的函數。
void *shmat(int _shmid,_const void *_shmaddr,int _shmflg);
int shmid是那塊共享內存的ID。
char *shmaddr是共享內存的起始地址
int shmflag是本進程對該內存的操作模式。如果是SHM_RDONLY的話,就是只讀模式。其它的是讀寫模式
成功時,這個函數返回共享內存的起始地址。失敗時返回-1。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
typedef struct
{
? ? int n;
? ? char str[256];
} ShmStru;
main()
{
? ? key_t lKey;
? ? int nShmId;
? ? ShmStru *pstru;
? ? if((lKey = ftok("/etc/profile",2)) < 0)
? ? {
? ? ? ? perror("ftok");
? ? ? ? exit(1);
? ? }
? ? if((nShmId = shmget(lKey,sizeof(ShmStru),IPC_CREAT|0666)) == -1)//創建共享內存
? ? {
? ? ? ? perror("shmget");
? ? ? ? exit(2);
? ? }
? ? if((pstru = shmat(nShmId,NULL,0)) == (void *)-1)//映射共享內存到本地
? ? {
? ? ? ? perror("shmat");
? ? ? ? exit(3);
? ? }
? ? pstru->n = 1;//修改共享內存
? ? strcpy(pstru->str,"123456");//向共享內存寫入數據
? ? if( shmdt(pstru) == -1)//解除共享內存映射
? ? {
? ? ? ? perror("shmdt");
? ? ? ? exit(4);
? ? }
? ? return 0;
}
3.shmdt刪除共享內存映射
shmdt()解除共享內存與進程地址空間的映射
int shmdt(_const void * _shmaddr);
參數char *shmaddr是那塊共享內存的起始地址
成功時返回0。失敗時返回-1。
4.shmctl控制共享內存映射
shmctl()函數如下:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int ? ?shmctl( int shmid , int cmd , struct shmid_ds *buf );
int shmid是共享內存的ID。
int cmd是控制命令,可取值如下:
? ? ? ?IPC_STAT ? ? ? ?得到共享內存的狀態
? ? ? ?IPC_SET ? ? ? ? ? 改變共享內存的狀態
? ? ? ?IPC_RMID ? ? ? ?刪除共享內存
struct shmid_ds *buf是一個結構體指針。IPC_STAT的時候,取得的狀態放在這個結構體中。如果要改變共享內存的狀態,用這個結構體指定
返回值:成功:0
? ? ? ? ? ? ? ?失敗:-1
?在使用共享內存,結束程序退出后。如果你沒在程序中用shmctl()刪除共享內存的話,一定要在命令行下用ipcrm命令刪除這塊共享內存。你要是不管的話,它就一直在那兒放著了。
簡單解釋一下ipcs命令和ipcrm命令。
取得ipc信息:
ipcs [-m|-q|-s]
-m ? ? ?輸出有關共享內存(shared memory)的信息
-q ? ? ?輸出有關信息隊列(message queue)的信息
-s ? ? ?輸出有關“遮斷器”(semaphore)的信息
%ipcs -m
刪除ipc
ipcrm -m|-q|-s shm_id
%ipcrm -m 105
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
typedef struct
{
? ? int n;
? ? char str[256];
} ShmStru;
main()
{
? ? key_t lKey;
? ? int nShmId;
? ? struct shmid_ds sds;
? ? if((lKey = ftok("/etc/profile",3)) == -1)
? ? {
? ? ? ? perror("ftok");
? ? ? ? exit(1);
? ? }
? ? if((nShmId = shmget(lKey,sizeof(ShmStru),IPC_CREAT|0666)) == -1)
? ? {
? ? ? ? perror("shmget");
? ? ? ? exit(2);
? ? }
? ? memset(&sds,0x00,sizeof(struct shmid_ds));
? ? if(shmctl(nShmId,IPC_STAT,&sds) < 0)//獲取共享內存屬性
? ? {
? ? ? ? perror("shmctl IPC_STAT");
? ? ? ? exit(3);
? ? }
? ? printf("First shm_perm.mode=0%o\n",sds.shm_perm.mode);
? ? sds.shm_perm.mode &= (~0002);//去除其他用戶的寫權限
? ? if(shmctl(nShmId,IPC_SET,&sds) < 0)
? ? {
? ? ? ? perror("shmctl IPC_SET");
? ? ? ? exit(4);
? ? }
? ? memset(&sds,0x00,sizeof(struct shmid_ds));
? ? if(shmctl(nShmId,IPC_STAT,&sds) < 0)
? ? {
? ? ? ? perror("shmctl IPC_STAT");
? ? ? ? exit(5);
? ? }
? ? printf("Second shm_perm.mode=0%o\n",sds.shm_perm.mode);//輸出共享內存的訪問權限信息
? ? if(shmctl(nShmId,IPC_RMID,NULL) < 0)//刪除共享內存
? ? {
? ? ? ? perror("shmctl IPC_RMID");
? ? ? ? exit(6);
? ? }
? ? return 0;
}
說明:?(~0222)取反后做與實際上就是去除其他用戶的寫權限,在C語言中,八進制常用用前綴表示
七.信號量
信號量是一種用于提供不同進程間或一個進程間的不同線程間線程同步手段的原語,systemV信號量在內核中維護
二值信號量 : 其值只有0、1 兩種選擇,0表示資源被鎖,1表示資源可用;
計數信號量:其值在0 和某個限定值之間,不限定資源數只在0 1 之間;
計數信號量集 ;多個信號量的集合組成信號量集
1.信號量集結構semid_ds
內核維護的信號量集結構信息如下
定義在頭文件<sys/sem.h>
? struct semid_ds {
? ? ? struct ipc_perm sem_perm; ? ? ? /* permissions .. see ipc.h */
? ? ? __kernel_time_t sem_otime; ? ? ?/* last semop time */
? ? ? __kernel_time_t sem_ctime; ? ? ?/* last change time */
? ? ? struct sem ?*sem_base; ? ? ?/* ptr to first semaphore in array */
? ? ? struct sem_queue *sem_pending; ? ? ?/* pending operations to be processed */
? ? ? struct sem_queue **sem_pending_last; ? ?/* last pending operation */
? ? ? struct sem_undo *undo; ? ? ? ? ?/* undo requests on this array */
? ? ? unsigned short ?sem_nsems; ? ? ?/* no. of semaphores in array */
? };
其中ipc_perm 結構是內核給每個進程間通信對象維護的一個信息結構,其成員包含所有者用戶id,所有者組id、創建者及其組id,以及訪問模式等;
semid_ds結構體中的sem結構是內核用于維護某個給定信號量的一組值的內部結構,其結構定義:
? struct sem {
? ? ? int semval; ? ? /* current value */
? ? ? int sempid; ? ? /* pid of last operation */
? ? ? struct list_head sem_pending; /* pending single-sop operations */
? };
? ? ? ?其中senval變量代表當前信號量的值,sempid 為最后一個成功操作該信號量的進程id,該結構體在內核以雙向鏈表進行 ?維護
semid_ds結構體中的sem_nsems成員代表該信號量標示符的信號量個數
求信號量極值
#?sysctl -a|grep sem
說明:輸出格式是 ?SEMMSL ? SEMMNS ?SEMOPM ?SEMMNI
每個信號量集最大信號量數目 ? 整個系統可以創建的信號量最大數目 ?每次semop對信號量操作的最大值 ?系統中可以創建的信號量集中的最大數目
2.semget創建信號量
int semget(key_t key, int nsems, int semflg);
該函數執行成功返回信號量標示符,失敗返回-1
參數key是通過調用ftok函數得到的鍵值,nsems代表創建信號量的個數,如果只是訪問而不創建則可以指定該參數為0,我們一旦創建了該信號量,就不能更改其信號量個數,只要你不刪除該信號量,你就是重新調用該函數創建該鍵值的信號量,該函數只是返回以前創建的值,不會重新創建;
semflg 指定該信號量的讀寫權限,當創建信號量時不許加IPC_CREAT ,若指定IPC_CREAT |IPC_EXCL則創建是存在該信號量,創建失敗;
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sem.h>
main()
{
? ? int semid;
? ? key_t semkey;
? ? if((semkey = ftok("/etc/profile",1)) < 0)
? ? {
? ? ? ? perror("ftok");
? ? ? ? exit(1);
? ? }
? ? if((semid = semget(semkey,1,IPC_CREAT|0666)) < 0)//創建包含信號量的信號集,權限666,信號量集中的信號量數目是1個
? ? {
? ? ? ? perror("semget");
? ? ? ? exit(1);
? ? }
? ? printf("semid=%d\n",semid);
}
輸出:
3.semop改變信號量值
信號量操作是PV 操作,“互斥”與“同步”
?int semop(int semid, struct sembuf *sops, unsigned nsops);
該函數執行成功返回0,失敗返回-1;
第一個參數semid 為信號量標示符;nops為第二個參數的操作數組的個數,第二個參數sops為一個結構體數組指針,結構體定義在sys/sem.h中,結構體如下
? struct sembuf {
? ? ? unsigned short ?sem_num; ? ?/* semaphore index in array */
? ? ? short ? ? ? sem_op; ? ? /*信號量操作數 */
? ? ? short ? ? ? sem_flg; ? ?/*信號量操作標志 */
? };
sem_num 操作信號的下標,其值可以為0 到nops
sem_flg為該信號操作的標志:其值可以為0、IPC_NOWAIT 、 SEM_UNDO
0 ?在對信號量的操作不能執行的情況下,該操作阻塞到可以執行為止;
?IPC_NOWAIT 在對信號量的操作不能執行的情況下,該操作立即返回;
SEM_UNDO當操作的進程推出后,該進程對sem進行的操作將被取消;
sem_op取值 >0 則信號量加上它的值,等價于進程釋放信號量控制的資源
sem_op取值 =0若沒有設置IPC_NOWAIT, 那么調用進程 將進入睡眠狀態,直到信號量的值為0,否則進程直接返回
sem_op取值 <0則信號量加上它的值,等價于進程申請信號量控制的資源,若進程設置IPC_NOWAIT則進程再沒有可用資源情況下,進程 阻 塞,否則直接返回
3.semctl控制信號量
?int semctl(int semid, int semnum, int cmd, ...);
該函數執行成功返回非負值,失敗返回-1
參數semid為信號集的標識符;
參數 semnum標識一個特定信號,該參數僅用于 SETVAL、GETVAL、GETPID命令
cmd控制類型;
...說明函數參數是可選的,通過該共用體變量semun選擇操作參數,各字段如下:
? union semun {
? ? ? int val; ? ? ? ? ? ?/* SETVAL控制,用于設置信號量的值 */
? ? ? struct semid_ds __user *buf; ? ?/* 用于IPC_STAT & IPC_SET ,指向semid_ds結構指針,用于獲取或者設置信號量控制結構 */
? ? ? unsigned short __user *array; ? /*用于GETALL & SETALL,指向短整形數組指針,用于獲取或者設置信號量集的值 */
? ? ? struct seminfo __user *__buf; ? /* IPC_INFO控制命令,用于返回系統內核定義的信號量極值,為一結構指針,結構類型seminfo */
? };
struct seminfo {
int semmap;
int semmni;
int semmns;
int semmnu;
int semmsl;
int semopm;
int semume;
int semusz;
int semvmx;
int semaem;
};
semctl的cmd參數
?IPC_STAT讀取一個信號量集的數據結構semid_ds,并將其存儲在semun中的buf參數中。
?IPC_SET設置信號量集的數據結構semid_ds中的元素ipc_perm,其值取自semun中的buf參數。
?IPC_RMID將信號量集從系統中刪除
?GETALL用于讀取信號量集中的所有信號量的值,存于semnu的array中
?SETALL 設置所指定的信號量集的每個成員semval的值
?GETPID返回最后一個執行semop操作的進程的PID。
?LSETVAL把的val數據成員設置為當前資源數
?GETVAL把semval中的當前值作為函數的返回,即現有的資源數,返回值為非負數
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <string.h>
typedef union semun//semctl需要的
{
? ? int val;//保存信號量值
? ? struct semid_ds *buf;//信號量控制結構指針
? ? ushort *array;//無符號短整形變量
} SEMCTL_UNION;
main()
{
? ? int n,semid;//信號量標示符變量
? ? key_t semkey;//鍵值變量
? ? SEMCTL_UNION semctl_arg;//聯合類型變量
? ? struct sembuf buf;//semop調用所需的結構變量
? ? if((semkey = ftok("/etc/profile",1)) < 0)//創建鍵值
? ? {
? ? ? ? perror("ftok");
? ? ? ? exit(1);
? ? }
? ? if((semid = semget(semkey,1,0)) < 0)//創建信號量
? ? {
? ? ? ? perror("semget");
? ? ? ? exit(2);
? ? }
? ? semctl_arg.val = 2;//初始化
? ? if (semctl(semid,0,SETVAL,semctl_arg) < 0)//設置信號量初始值
? ? {
? ? ? ? perror("semctl");
? ? ? ? exit(3);
? ? }
? ? memset(&buf,0x00,sizeof(struct sembuf));//清空
? ? buf.sem_num = 0;//信號量序號從0開始,第一個
? ? buf.sem_op = -1;//P操作,所以-1
? ? buf.sem_flg = IPC_NOWAIT;//非阻塞
? ? for(n=0;;n++)//循環調用P操作,直到信號量變為0
? ? {
? ? ? ? if(semop(semid,&buf,1) == -1)//P操作
? ? ? ? {
? ? ? ? ? ? perror("semop");
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? printf("semop[%d]:current semphore value=%d\n",n,semctl(semid,0,GETVAL,semctl_arg));
? ? }
}
輸出:
==============================================================
擴展閱讀參考:
Linux 系統內核空間與用戶空間通信的實現與分析
Linux系統調用列表
系統調用跟我學(1)系統調用跟我學(2)系統調用跟我學(3)系統調用跟我學(4)
Linux環境進程間通信(二): 信號(上)
Linux環境進程間通信(二): 信號(下)
Linux環境進程間通信(四)信號燈
Linux環境進程間通信(三)消息隊列
Linux環境進程間通信(一)管道及有名管道
Linux環境進程間通信(五): 共享內存(上)
Linux環境進程間通信(五): 共享內存(下)
Linux 環境進程間通信(六)套接口
Linux 實時信號程序中鎖的探索
UNIX 共享內存應用中的問題及解決方法
對話 UNIX: 通過共享內存進行進程間通信
在 Linux 中使用共享對象 讓共享內存為您服務,而不是為您制造麻煩
Posix線程編程指南(1)?
Posix線程編程指南(2)
Posix線程編程指南(3)
Posix線程編程指南(4)
Posix線程編程指南(5)
Linux 上實現雙向進程間通信管道
POSIX 線程詳解 一種支持內存共享的簡捷工具
POSIX 線程詳解,第 2部分 稱作互斥對象的小玩意
POSIX 線程詳解,第 3 部分 使用條件變量提高效率
尚觀進程控制16
總結
以上是生活随笔為你收集整理的Linux进程间通信--进程,信号,管道,消息队列,信号量,共享内存的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《梦仙》第三十二句是什么
- 下一篇: Unix下C程序内存泄漏检测工具Valg