1. 程式人生 > >linux 下C語言程式設計(2)——程序的建立,掛起,解掛,程序間通訊

linux 下C語言程式設計(2)——程序的建立,掛起,解掛,程序間通訊

在 linux 下利用C語言實現程序的建立,掛起和解掛操作

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>
#include <errno.h>
#include <stdlib.h>
/***********************************************************

    功能說明:在 linux 下利用C語言實現程序的建立,掛起和解掛操作
    author: 

***********************************************************/
void waitprocess(); int main(int argc, char * argv[]) { waitprocess(); } void waitprocess() { int count = 0; pid_t pid = fork(); int status = -1; if(pid<0) { printf("fork錯誤因為 %m\n",errno ); }else if(pid>0) { printf("這是父程序 pid = %d\n",getpid() ); printf("父程序 掛起\n"
); wait(&status);//父程序執行到此,馬上阻塞自己,直到有子程序結束 }else { printf("建立 子程序\n"); printf("這是子程序 pid = %d ,父程序為 ppid = %d\n",getpid(),getppid() ); int i; for (i = 0; i < 10; i++) { count++; sleep(1); printf("父程序已掛起 %d 秒\n", count) ; } exit(5); } printf
("子程序的退出狀態exit status 為 %d\n", WEXITSTATUS(status));//status是按位儲存的狀態資訊 printf("父程序 解掛\n"); printf("從 pid = %d\(父程序\) 結束程式\n",getpid() ); }

這裡寫圖片描述

程序間通訊一:使用訊息佇列

這裡寫圖片描述

msgsend.c:

    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/msg.h>
    #include <errno.h>

    #define MAX_TEXT 512  
    struct msg_st
    {
        long int msg_type;  
        char text[MAX_TEXT];
    };

    int main()
    {
        int running = 1;  
        struct msg_st data; 
        char buffer[BUFSIZ];
        int msgid = -1;

        //建立訊息佇列  
        msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
        if(msgid == -1)
        {
            fprintf(stderr, "msgget failed with error: %d\n", errno);
            exit(EXIT_FAILURE);
        }

        //向訊息佇列中寫訊息,直到寫入end  
        while(running)
        {
            //輸入資料  
            printf("Enter some text: "); 
            fgets(buffer, BUFSIZ, stdin);  
            data.msg_type = 1;    //注意2  
            strcpy(data.text, buffer);
            //向佇列傳送資料  
            if(msgsnd(msgid, (void*)&data, MAX_TEXT, 0) == -1)
            {
                fprintf(stderr, "msgsnd failed\n");
                exit(EXIT_FAILURE);
            }  
            //輸入end結束輸入
            if(strncmp(buffer, "end", 3) == 0)
                running = 0;
            sleep(1);
        }
        exit(EXIT_SUCCESS);
    }


msgsend.c:

    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <sys/msg.h>

    struct msg_st
    {
        long int msg_type;
        char text[BUFSIZ];
    };

    int main()
    {
        int running = 1;
        int msgid = -1;
        struct msg_st data;
        long int msgtype = 0; //注意1  

        //建立訊息佇列  
        msgid = msgget((key_t)1234, 0666 | IPC_CREAT);
        if(msgid == -1)
        {
            fprintf(stderr, "msgget failed with error: %d\n", errno);
            exit(EXIT_FAILURE);
        }
        //從佇列中獲取訊息,直到遇到end訊息為止  
        while(running)
        {
            if(msgrcv(msgid, (void*)&data, BUFSIZ, msgtype, 0) == -1)
            {
                fprintf(stderr, "msgrcv failed with errno: %d\n", errno);
                exit(EXIT_FAILURE);
            }
            printf("You wrote: %s\n",data.text);
            //遇到end結束  
            if(strncmp(data.text, "end", 3) == 0)
                running = 0;
        }
        //刪除訊息佇列  
        if(msgctl(msgid, IPC_RMID, 0) == -1)
        {
            fprintf(stderr, "msgctl(IPC_RMID) failed\n");
            exit(EXIT_FAILURE);
        }
        exit(EXIT_SUCCESS);
    }

程序間通訊二:使用共享記憶體

這裡寫圖片描述

shmdata.h:

    #ifndef _SHMDATA_H_HEADER  
    #define _SHMDATA_H_HEADER  

    #define TEXT_SZ 2048  

    struct shared_use_st  
    {  
        int written;//作為一個標誌,非0:表示可讀,0表示可寫  
        char text[TEXT_SZ];//記錄寫入和讀取的文字  
    };  

    #endif  

shmread.c:

    #include <unistd.h>  
    #include <stdlib.h>  
    #include <stdio.h>  
    #include <sys/shm.h>  
    #include "shmdata.h"  

    int main()  
    {  
        int running = 1;//程式是否繼續執行的標誌  
        void *shm = NULL;//分配的共享記憶體的原始首地址  
        struct shared_use_st *shared;//指向shm  
        int shmid;//共享記憶體識別符號  
        //建立共享記憶體  
        shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666|IPC_CREAT);  
        if(shmid == -1)  
        {  
            fprintf(stderr, "shmget failed\n");  
            exit(EXIT_FAILURE);  
        }  
        //將共享記憶體連線到當前程序的地址空間  
        shm = shmat(shmid, 0, 0);  
        if(shm == (void*)-1)  
        {  
            fprintf(stderr, "shmat failed\n");  
            exit(EXIT_FAILURE);  
        }  
        printf("\nMemory attached at %X\n", (int)shm);  
        //設定共享記憶體  
        shared = (struct shared_use_st*)shm;  
        shared->written = 0;  
        while(running)//讀取共享記憶體中的資料  
        {  
            //沒有程序向共享記憶體定資料有資料可讀取  
            if(shared->written != 0)  
            {  
                printf("You wrote: %s", shared->text);  
                sleep(rand() % 3);  
                //讀取完資料,設定written使共享記憶體段可寫  
                shared->written = 0;  
                //輸入了end,退出迴圈(程式)  
                if(strncmp(shared->text, "end", 3) == 0)  
                    running = 0;  
            }  
            else//有其他程序在寫資料,不能讀取資料  
                sleep(1);  
        }  
        //把共享記憶體從當前程序中分離  
        if(shmdt(shm) == -1)  
        {  
            fprintf(stderr, "shmdt failed\n");  
            exit(EXIT_FAILURE);  
        }  
        //刪除共享記憶體  
        if(shmctl(shmid, IPC_RMID, 0) == -1)  
        {  
            fprintf(stderr, "shmctl(IPC_RMID) failed\n");  
            exit(EXIT_FAILURE);  
        }  
        exit(EXIT_SUCCESS);  
    }  

shmwrite.c:

    #include <unistd.h>  
    #include <stdlib.h>  
    #include <stdio.h>  
    #include <string.h>  
    #include <sys/shm.h>  
    #include "shmdata.h"  

    int main()  
    {  
        int running = 1;  
        void *shm = NULL;  
        struct shared_use_st *shared = NULL;  
        char buffer[BUFSIZ + 1];//用於儲存輸入的文字  
        int shmid;  
        //建立共享記憶體  
        shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666|IPC_CREAT);  
        if(shmid == -1)  
        {  
            fprintf(stderr, "shmget failed\n");  
            exit(EXIT_FAILURE);  
        }  
        //將共享記憶體連線到當前程序的地址空間  
        shm = shmat(shmid, (void*)0, 0);  
        if(shm == (void*)-1)  
        {  
            fprintf(stderr, "shmat failed\n");  
            exit(EXIT_FAILURE);  
        }  
        printf("Memory attached at %X\n", (int)shm);  
        //設定共享記憶體  
        shared = (struct shared_use_st*)shm;  
        while(running)//向共享記憶體中寫資料  
        {  
            //資料還沒有被讀取,則等待資料被讀取,不能向共享記憶體中寫入文字  
            while(shared->written == 1)  
            {  
                sleep(1);  
                printf("Waiting...\n");  
            }  
            //向共享記憶體中寫入資料  
            printf("Enter some text: ");  
            fgets(buffer, BUFSIZ, stdin);  
            strncpy(shared->text, buffer, TEXT_SZ);  
            //寫完資料,設定written使共享記憶體段可讀  
            shared->written = 1;  
            //輸入了end,退出迴圈(程式)  
            if(strncmp(buffer, "end", 3) == 0)  
                running = 0;  
        }  
        //把共享記憶體從當前程序中分離  
        if(shmdt(shm) == -1)  
        {  
            fprintf(stderr, "shmdt failed\n");  
            exit(EXIT_FAILURE);  
        }  
        sleep(2);  
        exit(EXIT_SUCCESS);  
    }