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 = -1217050944
new 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
原创粉丝点击