Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Testing your System Call To test your system call, write a simple program which

ID: 3902660 • Letter: T

Question

Testing your System Call

To test your system call, write a simple program which creates a few child processes and a few child threads before it  calls the new pinfo system call.Your program should print all the process state information for the calling process. Run the program several times. Which fields in the pinfo structure change? Which ones do not? For each field in the pinfo structure, discuss how frequently it changes and why as part of your experiments and document in your learning report.

Although system calls are generally accessed through a library (glibc), your test program should access your system call directly. This is accomplished by utilizing the syscall() function. See man syscall. Your test program may include code like the following:

#include <unistd.h>

#include <stdio.h>

#include “pinfo.h”

Int main()

{

  struct pinfo p_info;

  int status;

  … /* create child processes and keep ‘em active */

  … /* create child threads and keep’em active */

  /* If this is the parent process, call new system call… */

  status = syscall (327, &p_info);    //NOTE: use proper system call number designated in the system call table file (/usr/src/linux/arch/x86/syscalls/syscall_64.tbl)        

/* … and output to standard output the info returned by new system call */

    printf("p_info: ");

    printf(

        "pid = %d "

        "state = %ld "

        "nice = %ld "

        "parent_pid = %d "

        "children = %d "

        "youngest_child_pid = %d "

        "younger_sibling_pid = %d "

        "older_sibling_pid = %d "

        "start_time = %lu "

        "user_time = %ld "

        "sys_time = %ld "

        "uid = %ld "

        "comm = %s ",

        p_info.pid, p_info.state, p_info.nice, p_info.parent_pid, p_info.children,

        p_info.youngest_child_pid, p_info.younger_sibling_pid, p_info.older_sibling_pid,

        p_info.start_time, p_info.user_time, p_info.sys_time, p_info.uid, p_info.comm);

exit(0);

}

Consider setting up a shell script from which you start several background processes (i.e. sleep x) prior to launching your testing C program so as to register siblings with a common parent. The child processes and child threads created from within your testing C program should block or sleep long enough to allow the new system call to collect statistics for their parent.

The output of the program should be easy to read. The ps and top commands will provide valuable help in verifying the accuracy of information printed by your program. You can access detailed information on these commands on the man pages.

Explanation / Answer

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <sys/wait.h>

int main(void)

{

pid_t child_pid, wpid;

int status = 0;

int i;

int a[3] = {1, 2, 1};

printf("parent_pid = %d ", getpid());

for (i = 0; i < 3; i++)

{

printf("i = %d ", i);

if ((child_pid = fork()) == 0)

{

printf("In child process (pid = %d) ", getpid());

if (a[i] < 2)

{

printf("Should be accept ");

exit(1);

}

else

{

printf("Should be reject ");

exit(0);

}

/*NOTREACHED*/

}

}

while ((wpid = wait(&status)) > 0)

{

printf("Exit status of %d was %d (%s) ", (int)wpid, status,

(status > 0) ? "accept" : "reject");

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote