1 / 16

Using System Calls (Unix)

Using System Calls (Unix). Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where the library itself lives Use Unix “man” pages to tell you about this for each system call Other resources. Recall Parameters to main in C.

cnickell
Download Presentation

Using System Calls (Unix)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using System Calls (Unix) • Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files • May have to tell compiler where the library itself lives • Use Unix “man” pages to tell you about this for each system call • Other resources

  2. Recall Parameters to main in C. main(int argc, char *argv[]) //also char *envp[] { ………………………………..} argc: Number of command line parameters (always at least 1). argv: Array of pointers to command line parameters. Each parameter is a null terminated string.

  3. Example: % ./a.out 1 4 main(int argc, char *argv[]) { ………………………………..} argc = 3 argv points to an array with three elements, each of which is a pointer to a null terminated string.

  4. argv “a.out” “1” “4” Example: % ./a.out 1 4

  5. The name of the program is always contained in element 0 of argv. Ex: Want to print out name of program (inefficiently): char *ptr ; ptr = argv[0] ; while (*ptr != ‘\0’) printf(“%c”, *ptr++) ;

  6. Exec Family of System Calls • Used to begin a processes execution. • Overwrites process (core) image of the calling process with that of called program. • Several flavors, use the one most suited to needs. • int execv( char *path, char *argvec[]) ; • int execl(const char *path, const char *arg0, ..., const char *argn, char * /* NULL*/);

  7. exec Function calls int execv( char *path, char *argvec[]) ; pathname: Can be an executable program in your directory (application code) or a system program such as ls, cd, date, ………….. argvec: Array of pointers to NULL terminated strings. First element should always be the name of the program, last element should always be NULL.

  8. main (int argc, *argv[]) { int my_pid ; char *args[3] ; my_pid = fork() ; if (my_pid ==0) { args[0] = “./a.out” ; All executed by args[1] = “2” ; child process args[2] = NULL; execv(“./a.out”, args) ; printf(“OOOpppssss.\n”) ; } else printf(“Not a problem!\n”) ; Executed by parent }

  9. Program a.out will begin to execute and will have the following parameters to main: argc = 3. argv[0] = ./a.out argv[1] = “2”

  10. Process Management System Calls • wait() Blocks parent process until all child processes have terminated • wait(pid) Blocks until a particular child process terminates. • Exit() Terminate the process and de-allocate its resources.

  11. Very Simple Shell while(1) { type_prompt() ; read_command(command, parameters) ; if (fork() != 0) //I am the parent wait() ; else { execv(command, parameters) ; printf(“OOOPPPSSS\”) ; } }

  12. File Management System Calls • int fd = open(name, how, mode); • “name” is pathname • Mode is file permissions. • “how” is an integer whose bits tell the system call how to open the file: O_RDONLY: read only (0) O_WRONLY: write only (1) O_RDWR: read and write (2) • int s = close(fd);

  13. File Management System Calls • int n = read(int fd, void *buffer, int nbytes); • fd: file descriptor • buffer: destination • nbytes: how many bytes to read • n: number read, or 0 on EOF • file position is incremented • int n = write(int fd, void *buffer, int nbytes);

  14. File Management System Calls • pos = lseek(fd,offset,whence); • “offset” and “pos” are of type off_t (on Solaris), which is really a long integer • “whence” determines what offset means: • SEEK_SET: file position is set to “offset” bytes • SEEK_CUR: file position is set to current position + “offset” • SEEK_END: file position is set to end of file + “offset” • pos: new file position

  15. Reading from stdin • scanf (char *, &var) reads from the terminal according to format string (spaces ignored). Example: int j, k ; scanf(“%d”, &j) ; read first arg, store as int. scanf(“%d”, &k) ;// read second arg, store as int.

  16. char x, *buf, *buf1 ; buf = (char *) malloc(1024) ; buf1 = (char *) malloc(1024) ; scanf(“%s”, buf) ; scanf(“%s”, buf1) ;

More Related