zoukankan      html  css  js  c++  java
  • linuc c 代码示例

    fork的应用:

    #include "stdio.h"
    #include "string.h"
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
     
    #include <unistd.h>
    
    #define Max(a,b) ({int a1=a,b1=b; a1>b1?a1:b1;})
    
    int main()
    {
    
        int i,j,k,*q;
        char *p="abc";
        int pids[10];
        int pid;
    
        q=pids;
    
        //printf("%d 
    ", getpid());
        //sleep (3);
    
    for(i=0;i<3;i++)
    {
    
        printf("for-%d
    ",i);
        pid=fork();
        if(pid) {
            *(q++) = pid;    
        }else if(pid == 0){
            sleep(5);
            printf("child:%d 
    ", getpid());
            break;
        }
    }
        if(pid>0)
        {
            for(i=0;i<3;i++)
            {
            //    waitpid(pids[i], NULL, 0);    
                printf("parent: %d, %d 
    ", getpid(),pids[i]);
    
            }
        }
        printf("song 
    ");
    
    }

    没有waitpid时输出:

    有waitpid时:

    pthread_create 函数示例:

    #include "stdio.h"
    #include "string.h"
    #include "pthread.h"
    
    void *thr_fn(void *arg)
    {
    
    sleep(15);
        printf("sub :%d 
    ", pthread_self());
        return NULL;
    }
    
    int main()
    {
        pthread_t err;
        pthread_t ntid;
    
        err = pthread_create(&ntid, NULL, thr_fn, NULL);
        if (err != 0)
            printf("can't create thread: %s
    ", strerror(err));
        //pthread_join(ntid,NULL);
    
        printf("main :%d 
    ", pthread_self());
    sleep(16);
        return 0;
    
    
    }
  • 相关阅读:
    poj 2892 && 3468
    hdu 2444
    反素数
    poj 2406 && 1961
    Java定时器Timer的使用
    Linux环境下如何生成core文件
    Java异步CompletableFuture的使用
    Java中线程池的使用
    Linux系统添加应用服务进程的守护进程
    C++通过Webbrowser调用JavaScript
  • 原文地址:https://www.cnblogs.com/siqi/p/4060834.html
Copyright © 2011-2022 走看看