![](https://img-blog.csdnimg.cn/20201014180756913.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Linux下 C
文章平均质量分 77
Jason20075563
这个作者很懒,什么都没留下…
展开
-
3、进程间通信-信号
1、使用sigaction函数 安装信号(signal函数也可以)code:#include #include #include void myHandler(int sig); int main() { struct sigaction act,oact; act.sa_handler=myHandler; sigemptyset(&act.sa_mask); act.sa_flags=0; sigaction(SIGUSR1,&act,&oact);原创 2010-11-27 21:57:00 · 744 阅读 · 0 评论 -
1、进程间通信-PIPE(无名管道)
1、在子进程中写入数据,在父进程中读取数据code:#include#include#includeint main(){ pid_t pid; int temp; int pipedes[2]; char s[14]="Jason's message"; char d[14]; if((pipe(pipedes))==-1){ perror("pipe"); exit(EXIT_FAILURE); } if((pid=fork())==-1){原创 2010-11-22 15:55:00 · 790 阅读 · 0 评论 -
Linux进程管理与程序开发
1、创建进程code:#include#include#includeint main(){ pid_t pid; if((pid=fork())==-1) puts("fork error"); else if(pid==0) puts("in the child"); else puts("in the parent"); return 0;}结果:in the parentin the child原创 2010-11-18 17:01:00 · 521 阅读 · 0 评论 -
文件及目录管理 POSIX C
<br />1、打印打开文件的描述符<br />code:<br />#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<fcntl.h>#include<sys/types.h>#include<sys/stat.h>int main(int argc,char * argv[]){ int fp1,fp2; printf("no is/t%d/n",fileno(stdin)); printf原创 2010-11-17 15:32:00 · 962 阅读 · 0 评论 -
ANSIC文件管理
1、复制一个文件,产生备份,使用了缓冲区code:#include#include#includeint main(int argc,char * argv[]){ FILE* fp_src,*fp_des; int i; char buffer[10]; if((fp_src=fopen(argv[1],"r+"))==NULL){ perror("open1"); exit(EXIT_FAILURE); } if((fp_des=fopen(ar原创 2010-11-16 21:45:00 · 767 阅读 · 0 评论 -
线程
p { margin-bottom: 0.21cm; }1、创建线程,使用pthread_create函数创建新线程,并使用结构体传递了多个参数codes:root@ubuntu:/code/chap10# cat test1.c#include#include#include#includestruct message{ int i; int j;};void * hello(struct message* str原创 2010-12-06 14:18:00 · 553 阅读 · 0 评论 -
6、进程间通信-共享内存
<br />1、父子进程通过共享内存进行数据通信。父子进程通过竞争方式来创建一个共享内存单元,然后子进程接受用户输入的信息,并将其写入到共享内存单元;父进程则从共享内存单元将该信息读出。<br />code:<br />root@ubuntu:/code/chap9# cat test4.c#include<errno.h>#include<fcntl.h>#include<stdio.h>#include<string.h>#include<unistd.h>#include<sy原创 2010-12-02 15:30:00 · 1221 阅读 · 1 评论 -
5、进程间通信-信号量
1、一个读取设置信号量集合的示例程序code:#include#include#include#include#define MAX_SEMAPHORES 5int main(){ union semun // 参数联合体 { int val; struct semid_ds * buf; unsigned short * array; //for GETALL and SETALL } arg; int semid=semget(IPC_PRIVA原创 2010-12-01 15:04:00 · 627 阅读 · 0 评论 -
4、进程间通信-消息队列IPC
1、创建一个消息队列,然后使用msgctl函数读取相关信息,单进程code:#include#include#include#include#include#include#include#define BUFSIZE 128// 自定义消息结构体struct msg_buf{ long type; char msg[BUFSIZE];};int main(){ key_t key; int msgid; struct msg_buf原创 2010-11-30 14:28:00 · 1013 阅读 · 0 评论 -
2、进程间通信-FIFO(有名管道)
1、具有亲缘关系的进程,使用FIFO通信父进程向管道写入数据,子进程从管道中读出数据code:#include#include#include#include#include#include#define FIFO "/tmp/fifo"int main(){ pid_t pid; char buffer[80]; int fd; unlink(FIFO); //如果文件存在,先删除此文件 mkfifo(FIFO,0744); //创建管道 if((p原创 2010-11-24 16:52:00 · 770 阅读 · 0 评论