Friday, December 3, 2010

Some system functions for Linux system programming in C


1) Get process id and parent process id:
// using function getpid() and getppid()
#include < stdio.h >
#include < unistd.h >

int main ()
{
  int process_id = getpid();
  int parent_process_id = getppid();
  printf ("The process id is %d\n", process_id);
  printf ("The parent process id is %d\n", parent_process_id);
  return 0;
}
2. system command
#include <stdlib.h>
//using system function to run commands similar to shell
int main ()
{
 int sys=system ("who");
  return (0);
}
3.fork function
fork - create a new process
Synopsis

#include <unistd.h>

pid_t fork(void);
Description The fork() function shall create a new process

#include <stdio.h>   /* printf, stderr, fprintf */
#include <unistd.h>  /* _exit, fork */
#include <stdlib.h>  /* exit */

#include <errno.h>   /* errno */
 
int main(void)
{
   pid_t  pid;

 
   /* Output from both the child and the parent process
    * will be written to the standard output,
    * as they both run at the same time.
    */
   pid = fork();
   if (pid == 0)

   {
      /* Child process:
       * When fork() returns 0, we are in
       * the child process.
       * Here we count up to ten, one each second.
       */
      int j;
      for (j = 0; j < 10; j++)

      {
         printf("child: %d\n", j);
         sleep(1);

      }
      _exit(0);  /* Note that we do not use exit() */
   }
   else if (pid > 0)

   { 
      /* Parent process:
       * When fork() returns a positive number, we are in the parent process
       * (the fork return value is the PID of the newly-created child process).
       * Again we count up to ten.
       */
      int i;
      for (i = 0; i < 10; i++)

      {
         printf("parent: %d\n", i);
         sleep(1);

      }
      exit(0);
   }
   else
   {   
      /* Error:
       * When fork() returns a negative number, an error happened
       * (for example, number of processes reached the limit).
       */

      fprintf(stderr, "can't fork, error %d\n", errno);
      exit(EXIT_FAILURE);

   }
}

4.malloc function

malloc is a subroutine for performing dynamic memory allocation in the C and C++ programming languages, though its use in C++ has been largely superseded by operators new and new[]. malloc is part of the standard library for both languages and is declared in the stdlib.h header although it is also declared within the std namespace via the C++'s cstdlib header

#include <fcntl.h>
#include <stdlib.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main(){

  const char* filename ="temp.txt";

  size_t length=1024;
  char* buffer;
  int fd;

  ssize_t bytes_read;

  /* Allocate the buffer.  */
  buffer = (char*) malloc (length);

  if (buffer == NULL)
    return 1;

  /* Open the file.  */
  fd = open (filename, O_RDONLY);

  if (fd == -1) {
    /* open failed.  Deallocate buffer before returning.  */
    free (buffer);

    return 1;
  }
  /* Read the data.  */
  bytes_read = read (fd, buffer, length);

   printf("%d\n", bytes_read);
  //output buffer
  printf("%s\n", buffer);

  free (buffer);
  close (fd);
  return 0;
}

No comments:

Post a Comment