原文地址:
http://www.cnblogs.com/hebaichuanyeah/p/3349061.html
感觉linux环境下C编程主要是一堆库函数的调用。
一.关于linux进程控制
关于进程的创建。
linux中fork()和vfork()函数可以创建一个子进程。
其中vfork()创建的子进程与父进程共享数据,仅子进程先与父进程运行。
代码:fork()创建子进程
#include "unistd.h" #include "stdio.h" main() { pid_t result; result = fork(); if(result == -1) printf("error"); else if(result == 0) printf(" result:%d in child process pid:%d ",result,getpid()); else printf("result:%d in father process pid:%d",result,getpid()); }
代码:由于vfork()创建的子进程与父进程共享数据
#include "stdio.h" #include "unistd.h" main() { pid_t pid; int count = 0; pid = vfork(); count++; if(pid == -1) printf("error"); else if(pid == 0) printf("in child process count is:%d ",count); else printf("in father process count is:%d ",count); }
关于exec函数族
exec函数族,可以在一个进程中启动另一个程序,当该进程不能为系统和用过贡献时,可以调用exec族函数执行另一个程序。
代码:execl调用编译后的helloworld.c文件
#include "stdio.h" #include "unistd.h" main() { if(execl("/home/he/program/ctest/helloworld",NULL)<0) printf("error "); }
关于wait()与waitpid()函数。
wait()与waitpid可以阻塞父进程。
pid_t wait(int *status)
waitpid原型
代码:waitpid使用
#include "unistd.h" #include "stdio.h" #include "sys/types.h" #include "sys/wait.h" main() { pid_t result; int pr; result = fork(); if(result == -1) printf("error "); else if(result == 0) { sleep(5); printf(" result:%d in child process pid:%d ",result,getpid()); } else { while(!(pr=waitpid(result,NULL,WNOHANG))) { sleep(1); printf("the child process not exit "); } printf("result:%d in father process pid:%d ",result,getpid()); } }
关于linux守护进程
守护进程是linux后台服务进程,该进程独立于终端,周期性执行某种任务。
代码:编写守护进程
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<fcntl.h> #include<sys/types.h> #include<unistd.h> #include<sys/wait.h> #include<syslog.h> #define MAXFILE 65535 int main() { pid_t pc,sid; int i,fd,len; char *buf="This is a Dameon "; len =strlen(buf); pc=fork(); if(pc<0) { printf("error fork "); exit(1); }else if(pc>0) exit(0); openlog("demo_update",LOG_PID, LOG_DAEMON); if((sid=setsid())<0) { syslog(LOG_ERR, "%s ", "setsid"); exit(1); } if((sid=chdir("/"))<0) { syslog(LOG_ERR, "%s ", "chdir"); exit(1); } umask(0); for(i=0;i<MAXFILE;i++) close(i); while(1) { if((fd=open("/tmp/dameon.log",O_CREAT|O_WRONLY|O_APPEND, 0600))<0) { syslog(LOG_ERR, "open"); exit(1); } write(fd, buf, len+1); close(fd); sleep(10); } closelog(); exit(0); }
该进程会10s周期向目标文件写入数据。
二.进程件的通讯
关于管道通讯
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int pipe_fd[2],New_process_result; char send_data[] = "This a message from father process"; char receive_data[100]; if(pipe(pipe_fd)<0) { printf("pipe create error "); } else printf("pipe create success "); if((New_process_result=fork()) == -1) { printf("Fork error "); } else if(New_process_result == 0)//子进程 { sleep(1); read(pipe_fd[0],receive_data,100); printf("%s",receive_data); putchar(' '); } else//父进程 { write(pipe_fd[1],send_data,strlen(send_data)); sleep(2); } }
在子进程中,打印父进程写入的数据。
关于FIFO有名管道
pipe管道只能用于父子进程通讯。
FIFO可以用于任意两个进程间的通讯。
fifo_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) { int fd; char w_buf[100]; char exitflag[] = "exit"; int nwrite; fd = open(FIFO_SERVER,O_RDWR|O_NONBLOCK,0); if(fd==-1) { perror("open error"); exit(1); } else printf("open success "); while(1) { scanf("%s",&w_buf[0]); if((nwrite=write(fd,w_buf,100))==-1) { if(errno==EAGAIN) printf("The FIFO has not been read yet.Please try later "); } else printf("write %s to the FIFO ",w_buf); if(!(strcmp(w_buf,exitflag))) exit(1); } }
fifo_read读数据文件
#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) { char buf_r[100]; char exitflag[] = "exit"; int fd; int nread; if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST)) printf("cannot create fifoserver "); printf("Preparing for reading bytes... "); 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)); read(fd,buf_r,100); if(buf_r[0] == '