2012.07.17

1pipe.c

#include <unistd.h>

#include <errno.h>

#include <stdio.h>

#include <stdlib.h>

int main()

{

       intpipe_fd[2];

       if(pipe(pipe_fd)<0)

       {

       printf("pipecreate error\n");

       return-1;

       }

       else

              printf("pipecreate success\n");

       close(pipe_fd[0]);

       close(pipe_fd[1]);

}

2pipe_rw.c

#include <unistd.h>

#include <sys/types.h>

#include <errno.h>

#include <stdio.h>

#include <stdlib.h>

int main()

{

       intpipe_fd[2];

       pid_tpid;

       charbuf_r[100];

       char*p_wbuf;

       intr_num;

       memset(buf_r,0,sizeof(buf_r));

       /*创建管道*/

       if(pipe(pipe_fd)<0)

       {

              printf("pipecreate error\n");

              return-1;

       }

       /*创建子进程*/

       if((pid=fork())==0)  //子进程 OR 父进程?

       {

              printf("\n");

              close(pipe_fd[1]);

              sleep(2);/*为什么要睡眠*/

              if((r_num=read(pipe_fd[0],buf_r,100))>0)

              {

                     printf(   "%d numbers read from the pipe is%s\n",r_num,buf_r);

              }    

              close(pipe_fd[0]);

              exit(0);

      }

       elseif(pid>0)

       {

              close(pipe_fd[0]);

              if(write(pipe_fd[1],"Hello",5)!=-1)

                     printf("parentwrite1 Hello!\n");

              if(write(pipe_fd[1],"Pipe",5)!=-1)

                     printf("parentwrite2 Pipe!\n");

              close(pipe_fd[1]);

              sleep(3);

              waitpid(pid,NULL,0);/*等待子进程结束*/

              exit(0);

       }

       return0;

}

3fifo_read.c

#include <sys/types.h>

#include <sys/stat.h>

#include <errno.h>

#include <fcntl.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define FIFO "/tmp/myfifo"

main(int argc,char** argv)

{

       charbuf_r[100];

       int  fd;

       int  nread;

       /*创建管道 */

       if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))

              printf("cannotcreate fifoserver\n");

       printf("Preparingfor reading bytes...\n");

       memset(buf_r,0,sizeof(buf_r));

       /*打开管道 */

       fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);

       if(fd==-1)

       {

              perror("open");

              exit(1);   

       }

       while(1)

       {

              memset(buf_r,0,sizeof(buf_r));

              if((nread=read(fd,buf_r,100))==-1)

              {

                     if(errno==EAGAIN)

                            printf("nodata yet\n");

              }

              printf("read%s from FIFO\n",buf_r);

              sleep(1);

       }    

       pause();/*暂停,等待信号*/

       unlink(FIFO);//删除文件

}

4fifo_write.c

#include <sys/types.h>

#include <sys/stat.h>

#include <errno.h>

#include <fcntl.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define FIFO_SERVER "/tmp/myfifo"

main(int argc,char** argv)

{

       intfd;

       charw_buf[100];

       intnwrite;     

       /*打开管道*/

       fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);

       if(argc==1)

       {

              printf("Pleasesend something\n");

              exit(-1);

       }

       strcpy(w_buf,argv[1]);

       /*向管道写入数据 */

       if((nwrite=write(fd,w_buf,100))==-1)

       {

              if(errno==EAGAIN)

                     printf("TheFIFO has not been read yet.Please try later\n");

       }

       else

              printf("write%s to the FIFO\n",w_buf);

}

5shmem

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <errno.h>

#include <unistd.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#define PERM S_IRUSR|S_IWUSR

/* 共享内存 */

int main(int argc,char **argv)

{

       intshmid;

       char*p_addr,*c_addr;

       if(argc!=2)

       {

              fprintf(stderr,"Usage:%s\n\a",argv[0]);

              exit(1);

       }

       /*创建共享内存 */    

       if((shmid=shmget(IPC_PRIVATE,1024,PERM))==-1)

       {

              fprintf(stderr,"CreateShare Memory Error:%s\n\a",strerror(errno));

              exit(1);

       }

       /*创建子进程 */

       if(fork())// 父进程写

       {

              p_addr=shmat(shmid,0,0);

              memset(p_addr,'\0',1024);

              strncpy(p_addr,argv[1],1024);

              wait(NULL);// 释放资源,不关心终止状态

              exit(0);

       }

       else       // 子进程读

       {

              sleep(1);// 暂停1          

              c_addr=shmat(shmid,0,0);

              printf("Clientget %p\n",c_addr);

              exit(0);

       }

}

6mysignal.c

#include <signal.h>

#include <stdio.h>

#include <stdlib.h>

void my_func(int sign_no)

{

       if(sign_no==SIGINT)

              printf("Ihave get SIGINT\n");

       elseif(sign_no==SIGQUIT)

              printf("Ihave get SIGQUIT\n");

}

int main()

{

       printf("Waitingfor signal SIGINT or SIGQUIT \n ");

       /*注册信号处理函数*/

       signal(SIGINT,my_func);

       signal(SIGQUIT,my_func);

       pause();

       exit(0);

}

7sigaction.c

#include <sys/types.h>

#include <unistd.h>

#include <signal.h>

#include <stdio.h>

#include <stdlib.h>

void my_func(int signum)

{

       printf("Ifyou want to quit,please try SIGQUIT\n");

}

int main()

{

       sigset_tset,pendset;

       structsigaction action1,action2;

              if(sigemptyset(&set)<0)

              perror("sigemptyset");

              if(sigaddset(&set,SIGQUIT)<0)

              perror("sigaddset");

              if(sigaddset(&set,SIGINT)<0)

              perror("sigaddset");

              if(sigprocmask(SIG_BLOCK,&set,NULL)<0)

              perror("sigprocmask");

       else

       {

              printf("blocked\n");

              sleep(5);

       }

       if(sigprocmask(SIG_UNBLOCK,&set,NULL)<0)

              perror("sigprocmask");

       else

              printf("unblock\n");

       while(1)

       {

              if(sigismember(&set,SIGINT))

              {

                     sigemptyset(&action1.sa_mask);

                     action1.sa_handler=my_func;

                     sigaction(SIGINT,&action1,NULL);

              }

              elseif(sigismember(&set,SIGQUIT))

              {

                     sigemptyset(&action2.sa_mask);

                     action2.sa_handler= SIG_DFL;

                     sigaction(SIGTERM,&action2,NULL);

              }

       }

}

8msg.c

#include <sys/types.h>

#include <sys/msg.h>

#include <unistd.h>

struct msg_buf

    {

       int mtype;

       char data[255];

   };

int main()

{

       key_t key;

       int msgid;

       int ret;

       struct msg_buf msgbuf;

       key=ftok("/tmp/2",'a');

       printf("key =[%x]\n",key);

       msgid=msgget(key,IPC_CREAT|0666); /*通过文件对应*/

       if(msgid==-1)

       {

                printf("createerror\n");

                return -1;

       }

        msgbuf.mtype = getpid();

       strcpy(msgbuf.data,"test haha");

       ret=msgsnd(msgid,&msgbuf,sizeof(msgbuf.data),IPC_NOWAIT);

       if(ret==-1)

       {

                printf("send messageerr\n");

                return -1;

       }

        memset(&msgbuf,0,sizeof(msgbuf));

       ret=msgrcv(msgid,&msgbuf,sizeof(msgbuf.data),getpid(),IPC_NOWAIT);

       if(ret==-1)

       {

                printf("recv messageerr\n");

                return -1;

       }

       printf("recv msg =[%s]\n",msgbuf.data);

 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值