-3

How can i implement a UNIX system call like fork() or exec() in c programming language without making explicit calls. That is without using the system calls like fork(), exec() in the program code? As we all know the implementation of system calls by explicit calls.

#include <stdio.h>
#include <sys/wait.h>

int main()
{
   int p1,p2;

   p1 = fork();

   if(p1 == -1)
   {
       printf(“Error”);
       return 0;
   }
   else
   {
       printf(“parent is %d\n”, getppid());
       printf(“child is %d\n”, getpid());
   }

   p2 = fork();
   printf(“parent is %d\n”, getppid());
   printf(“child is %d\n”, getpid());
}

I just want a program that does the same function as above but without invoking fork() system call.

2
  • 3
    "plzz help me frndds!!" may be cool in kindergarten, but not on a professional Q & A site like this. Aug 8, 2014 at 6:02
  • 3
    You can't accomplish what system calls do without invoking system calls, because it involves getting the kernel to do something for you. That's the reason why system calls exist.
    – Brian Bi
    Aug 8, 2014 at 6:09

2 Answers 2

4

First of all, fork() and exec() are not system calls, they are library calls into the C runtime library. They might end up in system calls (On Linux, fork() and exec() most likely will be using the sys_fork and sys_execve system calls in the end, see this List of system calls for more information).

The system call is the interface to the kernel and is hightly architecture dependant (different parameter passing, different mechanisms to actually do the system call etc.). The C library is there to encapsulate these differences to allow writing portable applications.

If you (e.g. for learning purposes) still want to invoke a system call directly, you have two possibilities:

  1. Use inline assembly, e.g. something like the following to call the sys_exit system call on an i386 architecture:

    __asm__ ("movl $1, %eax\n\t"         /* System-call "sys_exit" */
             "movl $0, %ebx\n\t"         /* Exit-code 0 */
             "int  0x80");
    

    As you can see from the following example, the same system call is invoked in a completely different way on the x86_64 architecture:

    __asm__ ("mov $60, %rax\n\t"         /* System-call "sys_exit" */
             "mov $0, %rdi\n\t"          /* Exit-code 0 */
             "syscall");
    
  2. Use the syscall() function. See Accessing a system call directly from user program for more information.

1
  • 1
    The List of system calls link appears to be broken ... (extra paranthesis at end)
    – dragosht
    Aug 8, 2014 at 6:32
0

You cannot invoke system calls without explicitly calling them in C or any other language. You have to call them explicitly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.