Header Ads

System call

system( ) -
 function can be used to make a program run from within another program
thus creating a new process. The system( ) function runs the command passed to it and then waits for it to complete.


1)   Program to write some message on the screen.
                 #include<stdlib.h>
                 int main()
                 {
                   write(1, "hello", 5); // 1st parameter is file descriptor (1 is for                                 standard output), 2nd parameter is
                                    //the message and 3rd parameter is the length of the                                 message

                  }

Q2. Program to read input from the user and print it on the screen.

#include<stdlib.h>
int main()
{
   int n;
   char buff[50];
   n= read(0, buff, 50); // 1st parameter is file descriptor (0 is for standard input), 2nd parameter
                                     //is the buffer for storing the input and 3rd parameter is the max. possible
                                     //length of the input. n stores the number of characters read
   write(1, buff, n);

}




Powered by Blogger.