Multithreading lecture notes

2010-10-22  来源:本站原创  分类:OS  人气:117 

Resource allocation process is the smallest unit of the thread is the smallest unit of scheduling

Process must be assigned a separate address space, the new data table, stack and data segments

Follow multi-threaded POSIX thread interface, no longer needs the standard library header files pthread.h c
Connection requires libpthread.a--> gcc filename-lpthread

Create a thread

#include <pthread.h>
int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, void *(*start_rtn)(void), void *arg)
//tidp  Thread  ID
//attr  Thread properties, often empty
//start_rtn  Thread to perform the function
//arg start_rtn Parameters

Example;

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *create(void *arg)
{
    int *num;
    num=(int *)arg;
    printf("create parameter is %d \n",*num);
    return (void *)0;
}
int main(int argc ,char *argv[])
{
    pthread_t tidp;
    int error;

    int test=4;
    int *attr=&test;

    error=pthread_create(&tidp,NULL,create,(void *)attr);

    if(error)
        {
        printf("pthread_create is created is not created ... \n");
        return -1;
        }
    sleep(1);
    printf("pthread_create is created ...\n");
    return 0;
}

Terminate a thread in the process of any one thread exit or _exit, the process will terminate the thread exits the normal way:
1 From the start the routine returns
2 terminated by another process
3 pthread_exit function that calls

#include <pthread.h>
void pthread_exit(void *rval_ptr)
// Terminates the calling thread
//rval_ptr  The thread exits the return value of the pointer

Example:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *create(void *arg)
{
    printf("new thread is created ... \n");
    return (void *)8;
}

int main(int argc,char *argv[])
{
    pthread_t tid;
    int error;
    void *temp;

    error = pthread_create(&tid, NULL, create, NULL);
    printf("main thread!\n");

    if( error )
    {
        printf("thread is not created ... \n");
        return -1;
    }
    error = pthread_join(tid, &temp);

    if( error )
    {
        printf("thread is not exit ... \n");
        return -2;
    }

    printf("thread is exit code %d \n", (int )temp);
    return 0;
}
相关文章
  • Multithreading lecture notes 2010-10-22

    Resource allocation process is the smallest unit of the thread is the smallest unit of scheduling Process must be assigned a separate address space, the new data table, stack and data segments Follow multi-threaded POSIX thread interface, no longer n

  • Multithreading memo notes 2010-11-17

    "The art of multiprocessor programming" Appendix A fundamental problem that multi-threading. Including Java, C # and C (pthreads) implementation: thread creation, management process, thread-local objects, and the producer consumer problem solvin

  • Agility lecture notes 2010-11-07

    Shenzhen Kingdee Software Park today to participate in the "agile sharing sessions," notes the following: 1, automated testing support is necessary 2, agile only suitable for small projects? Transformation of key organizational agility ---------

  • Lecture Notes Execution 2010-07-31

    (Simple expression) execution of the measure - the amount and quality to complete its task. Enterprise core competitiveness lies in execution. Cycle phenomena Senior <----> middle <----> employees (mutual blaming poor execution) No one can by

  • Multithreading study notes 2010-08-31

    1.1 Brief History of Concurrency 1.three motivating factors of thread: Resource utilization: when there is a program waiting for input and output operation, for example, in order to allow another program to efficiently carry out its mandate. Fairness

  • Linux common commands lecture notes 2010-10-17

    ctrl + alt + F1-F7 to switch the console useradd username passwd username su - user name shutdown now cp-r source destination file cut, rename the file mv source destination file rm-r file folder name mkdir-p pwd cd ls-al folder name to create the fi

  • shell programming lecture notes 2010-10-17

    1. At the beginning: #! / Bin / bash 2. Executable: chmode + x filename 3. Note: # 4. Use of variables: set variable a a="hello" echo $a Prevent confusion $ {a} Default variables: $ #: Command-line arguments passed to the script number $ *: All

  • makefile lecture notes 2010-10-18

    GNU make: management project compile, link, target: prerequisites [Tab key] command Target: dependence [Tab] command main.o:main.c gcc -c main.c Known as pseudo-targets did not depend on:. PHONY declaration to clean fake target .PHONY : clean clean:

  • File programming lecture notes 2010-10-18

    Two ways: 1. System Call 2.C library System call 1. Create int creat(const char *filename, mode_t mode) creat(hello, 0775) 2. Open int open(const char *pathname, int flag) int open(const char *pathname, int flag, mode_t mode) flag: O_RDONLY open read

  • File programming (system calls) lecture notes 2010-10-18

    Two ways: 1. System Call 2.C library System call 1. Create int creat(const char *filename, mode_t mode) creat(hello, 0775) 2. Open int open(const char *pathname, int flag) int open(const char *pathname, int flag, mode_t mode) flag: O_RDONLY open read

  • File programming (library function) lecture notes 2010-10-18

    1. Open FILE *fopen(const char *filename, const char *mode) mode: r, rb read-only w, wb write-only, does not exist, create a, ab added, does not exist, create r +, r + b, rb + read and write w +, w + b, wh + read and write, does not exist, create a +

  • Time programming lecture notes 2010-10-18

    Calendar time: at 0:00 on January 1, 1970 to the present description header file: time.h 1. For calendar time time_t time(time_t *tloc) Return values and parameters are pointing time_t calendar time such as: time_t t=time(NULL); 2. Converted into GMT

  • Process programming lecture notes 2010-10-19

    1. To obtain ID #include <sys/types.h> #include <unistd.h> pid_t getpid(void) // Gets the process ID pid_t getppid(void) // Gets the parent process ID 2. Create child process #include <unistd.h> pid_t fork(void) Called once, back twice,

  • Process communication lecture notes 2010-10-20

    Inter-process communication (IPC) role 1. Data transmission 2. Commons 3. Notification events 4. Process Control IPC 6 way 1. Pipe (pipe), and named pipe (FIFO) 2. Signal (signal) 3. Message Queue 4. Shared Memory 5. Semaphore 6. Socket The tail pipe

  • Process communication - Signal lecture notes 2010-10-21

    Signal generation: 1 Press the button 2 hardware exception signal: the divisor is 0, an invalid memory access 3 Use the kill function to the signal sent to another thread 4, the signal sent by kill command to another thread Common signal: SIGUP the e

  • Process communication - shared memory lecture notes 2010-10-21

    Multiple processes sharing a physical memory between processes is the fastest way to share data Steps 1 Create a shared memory, use the shmget function 2 mapped shared memory, use the shmat function, the shared memory space is mapped to a specific pr

  • Interprocess communication - Semaphore lecture notes 2010-10-21

    Main purposes: A critical resource protection, access to critical resources, access to the semaphore before 2 Process Synchronization Category: Binary signal: just take the 0 and 1. As long as the sharing of resources are available, other processes c

  • Process communication - the message queue lecture notes 2010-10-22

    With the continuing key kernel #include <sys/types.h> #include <sys/ipc.h> key_t ftok(char *pathname, char proj) filename pathname proj project name, not 0 to return the file name corresponding to the key Open the message queue #include <sy

  • Network programming lecture notes 2010-10-23

    There are three socket types: SOCK_STREAM TCP + IP-based SOCK_DGRAM UDP + IP-based SOCK_RAW directly based on IP Network Address struct sockaddr network address for the record struct sockaddr{ u_short sa_family; char sa_data[14]; } //sa_family Protoc

  • Lecture notes based on network programming 2010-10-23

    There are three socket types: SOCK_STREAM TCP + IP-based SOCK_DGRAM UDP + IP-based SOCK_RAW directly based on IP Network Address struct sockaddr network address for the record struct sockaddr{ u_short sa_family; char sa_data[14]; } //sa_family Protoc