Thursday, August 26, 2010

Create a thread in C/C++



A thread of execution is the smallest unit of processing that can be scheduled by an operating system. It generally results from a fork of a computer program into two or more concurrently running tasks.


The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process. Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources.
compile:
g++  -lpthread thread.c

thread.c as following


-----------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *print_message( void *ptr )
{
   printf("%s \n", (char*) ptr);
}
int main()
{
     pthread_t thread1;
     char *m = "Thread 1";
     int  iret1;
    /* Create a  thread each to execute function */
    /* message1 pass to function print_message*/
iret1 = pthread_create( &thread1, NULL, print_message, (void*) m);
     /* pthread_join - wait for termination of another thread*/
     pthread_join( thread1, NULL);

     printf("Thread 1 returns: %d\n",iret1);

     return(0);

}




 ------------------------------------------------
 Pipes can be used in threads and processes. The program below
demonstrates how pipes can be used in processes. A new process can be
created using the system call fork(). It returns two differnt values to
the child and parent. The value 0 is returned to the child (new)
process and the PID (Process ID) of the child is returned to the parent
process. This is used to distinguish between the two processes. In the
program given below, the child process waits for the user input and
once an input is entered, it writes into the pipe. And the parent
process reads from the pipe.
 
Note here that the pipe() system call was called before the system call fork(). The buffer allocated to the pipe is accessible to both the processes.

g++ pipe_test.c
below is pipe_test.c 
 

#include <stdlib.h>
#include <stdio.h>

#include <unistd.h>

/* Write COUNT copies of MESSAGE to STREAM, pausing for a second
   between each.  */

void writer (const char* message, int count, FILE* stream)
{

  for (; count > 0; --count) {
    /* Write the message to the stream, and send it off immediately.  */
    fprintf (stream, "%s\n", message);

    fflush (stream);
    /* Snooze a while.  */
    sleep (1);
  }
}

/* Read random strings from the stream as long as possible.  */

void reader (FILE* stream)
{
  char buffer[1024];

  /* Read until we hit the end of the stream.  fgets reads until
     either a newline or the end-of-file.  */
  while (!feof (stream) 
  && !ferror (stream) 
  && fgets (buffer, sizeof (buffer), stream) != NULL) 
    fputs (buffer, stdout);
} 
int main ()
{
  int fds[2];
  pid_t pid;

  /* Create a pipe.  File descriptors for the two ends of the pipe are
     placed in fds.  */
  pipe (fds);
  /* Fork a child process.  */
  pid = fork ();

  if (pid == (pid_t) 0) {
    FILE* stream;

    /* This is the child process.  Close our copy of the write end of
       the file descriptor.  */
    close (fds[1]);
    /* Convert the read file descriptor to a FILE object, and read
       from it.  */
    stream = fdopen (fds[0], "r");

    reader (stream);
    close (fds[0]);
  }
  else {

    /* This is the parent process.  */
    FILE* stream;
    /* Close our copy of the read end of the file descriptor.  */
    close (fds[0]);

    /* Convert the write file descriptor to a FILE object, and write
       to it.  */
    stream = fdopen (fds[1], "w");

    writer ("Hello, world.", 5, stream);
    close (fds[1]);
  }

  return 0;
}

No comments:

Post a Comment