Unix thread sharing Create process Open file resource (1)

Source: Internet
Author: User

Execution Environment: Linux ubuntu 4.4.0-31-generic #50-ubuntu SMP Wed Jul 00:07:12 UTC x86_64 x86_64 x86_64 gnu/linux

1. Test code: A.C

1#include <fcntl.h>2#include <unistd.h>3#include <stdio.h>4#include <pthread.h>5#include <string.h>6 7 pthread_t Ntid;8 9 void* THR_FN (void*Arg)Ten { One    int* fp = (int*) arg; A    Char* str ="Hello the world!\n"; -  -    intLen = Write (*Fp,str,strlen (str)); the    if(len) -    { -printf"Thread Write a string to the file by the descriptor the CreateProcess created!\n"); -    } +  -Close (*FP); +    return((void*)0); A } at  - intMainvoid) - { -     interr; -     intFD; -  inFD = open ("Log.txt", o_creat|o_wronly); -     if(-1==FD) to     { +printf"Failed to open file!\n"); -     } the  *Err = Pthread_create (&ntid,NULL,thr_fn,&FD); $     if(Err! =0)Panax Notoginseng     { -printf"can ' t create thread, errno =%d\n", err);  the     } +Sleep3); A     return 0; the}

2. If the system does not have a corresponding Pthread library, execute:

1 sudo apt-get install glibc-doc

2 sudo apt-get install Manpages-posix-dev

Compile run:

1 gcc a.c-lpthread

Unix thread sharing Create process Open file resource (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.