UNIX环境高级编程之创建进程与线程
来源:互联网 发布:淘宝店雪亮卫生纸 编辑:程序博客网 时间:2024/05/01 18:19
</pre>1.创建进程<p></p><p>通过模拟system函数,学习父进程创建子进程过程。</p><p></p><pre name="code" class="objc"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="objc">#include <sys/wait.h>#include <unistd.h>#include <errno.h>#include <stdio.h>int system(const char * cmdstring){ pid_t pid; int status; if (NULL == cmdstring) { return 1; } if ((pid = fork()) < 0 ) { status = -1; } else if (0 == pid) { printf("child process entry\n"); printf("pid is %d, ppid is %d \n", getpid(), getppid()); execl("/bin/bash", "sh", "-c", cmdstring, (char *)0); printf("child process exit\n"); _exit(127); } else { printf("pid is %d, ppid is %d \n", getpid(), getppid()); printf("parent process entry\n"); while (waitpid(pid, &status, 0) < 0) { printf("parent wait\n"); if (errno != EINTR) { printf("status is %d\n", status); status = -1; break; } } printf("parent process exit\n"); }}void main(void){ system("/root/unixexcise/a.sh");}
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">输出结果:</span>
pid is 24869, ppid is 24223
parent process entry
child process entry
pid is 24870, ppid is 24869
000
000
parent process exit
2.线程
#include <pthread.h>#include <stdio.h>pthread_t ntid;void printids(const char * s){ pid_t pid; pthread_t tid; pid = getpid(); tid = pthread_self(); printf("%s pid = %ld, tid = %ld \n", s, (unsigned long)pid, (unsigned long)tid);}void * thr_fn(void * arg){ printids("new thread: "); pthread_exit((void *) 0);}int main(void){ int err; err = pthread_create(&ntid, NULL, thr_fn, NULL); if (err != 0) { printf("pthread_create err"); } printids("main thread:"); sleep(1); return 0;}
输出结果:
main thread: pid = 25370, tid = -1217050944new thread: pid = 25370, tid = -1217053840
3.线程锁:
#include <pthread.h>#include <stdio.h>pthread_t ntid;void printids(const char * s){ pid_t pid; pthread_t tid; pid = getpid(); tid = pthread_self(); printf("%s pid = %ld, tid = %ld \n", s, (unsigned long)pid, (unsigned long)tid);}void * thr_fn(void * arg){ printids("new thread: "); pthread_exit((void *) 0);}int main(void){ int err; err = pthread_create(&ntid, NULL, thr_fn, NULL); if (err != 0) { printf("pthread_create err"); } printids("main thread:"); sleep(1); return 0;}[root@localhost unixexcise]# ./a.out[root@localhost unixexcise]# gcc c.c -pthread[root@localhost unixexcise]# ./a.outmain thread: pid = 25370, tid = -1217050944new thread: pid = 25370, tid = -1217053840[root@localhost unixexcise]#[root@localhost unixexcise]#[root@localhost unixexcise]#[root@localhost unixexcise]# cat d.c#include <stdlib.h>#include <pthread.h>struct foo { int f_count; pthread_mutex_t f_lock; int f_id;};struct foo * foo_alloc(int id){ struct foo * fp; if ((fp = malloc(sizeof( struct foo))) != NULL) { fp->f_count = 1; fp->f_id = id; if (pthread_mutex_init(&fp->f_lock, NULL) != 0) { free(fp); return NULL; } return fp; }}void foo_hold(struct foo * fp){ pthread_mutex_lock(&fp->f_lock); fp->f_count++; pthread_mutex_unlock(&fp->f_lock);}void foo_rele(struct foo * fp){ if (NULL == fp) { return; } pthread_mutex_lock(&fp->f_lock); if (--fp->f_count == 0) { pthread_mutex_unlock(&fp->f_lock); pthread_mutex_destroy(&fp->f_lock); free(fp); } else { pthread_mutex_unlock(&fp->f_lock); }}void main(void){ struct foo * pst = foo_alloc(3); foo_hold(pst); foo_rele(pst);}
0 0
- UNIX环境高级编程之创建进程与线程
- UNIX环境高级编程——线程与进程区别
- UNIX环境高级编程——线程与进程区别
- UNIX环境高级编程之进程环境
- unix 环境高级编程 线程一 创建
- Unix环境高级编程之进程控制
- 《Unix环境高级编程》之 进程控制
- 《Unix环境高级编程》之 进程通信
- UNIX环境高级编程笔记之进程
- UNIX环境高级编程之线程
- UNIX环境高级编程笔记之线程
- 《unix高级环境编程》进程控制——创建进程
- 《unix高级环境编程》进程控制——创建进程
- UNIX环境高级编程学习之第九章进程关系-创建孤儿进程
- unix环境高级编程--进程环境与进程控制
- 《UNIX环境高级编程》第三部分进程之进程环境
- UNIX环境高级编程学习之第十一章线程-线程的创建、退出、等待、取消、分离
- UNIX环境高级编程学习之第十二章线程控制-以分离状态创建线程
- visual studio2012 配置Opencv
- myeclipse中tomcat端口被占用
- hdu 2680 最短路
- 1-1、Spark 的local模式安装
- Java中Vector和ArrayList的区别
- UNIX环境高级编程之创建进程与线程
- jQuery中prop()和attr()方法的测试和总结
- JSP语法
- 【HeadFirst】第七章:web开发[本地localhost访问][自动访问index.html]
- TCP/IP详解卷1 读书笔记:第二十八章 SMTP和POP3协议
- nginx + gunicorn + virtualenv部署django
- 导航栏的背景为不透明
- Java编写的重命名工具
- ITOO讨论会