Judge:System calls

From PEGWiki
Jump to: navigation, search

This page lists some common Linux system calls and discusses their usage in programs graded by the PEG Judge. Note that almost all of these have corresponding wrappers in libc without the "sys_" prefix (e.g., read for sys_read).

In general, the state of the filesystem is required to be identical before and after your program runs (except that the judge collects your output and automatically determines execution time and other statistics), and your program is not allowed to examine the filesystem at all except by reading from standard input. Thus, you may not create files, delete files, move files, open additional files, examine file attributes, nor change file attributes, nor may you do any of the same on directories; you also are not allowed to change working directory. Obviously, you should also not try to do anything that requires root privileges, such as increasing your resource limits.

  • sys_brk: This is essential for dynamic memory allocation, but you'll only ever want to call it directly if you're using assembly language; otherwise the programming language or standard libraries should be allowed to take care of it. Note that if you try to allocate more memory this way than the memory limit for your program, this call will fail.
  • sys_clone: See sys_fork.
  • sys_close: This does not necessarily have to be called, as the kernel automatically closes all open file descriptors when a program terminates in any way. However, it is almost always called indirectly when a program terminates (unless this program is written in assembly language).
  • sys_execve: Not allowed. Your program may not invoke any other programs. If it tries, either it will be killed, or the system call will simply fail.
  • sys_exit: Every program must sys_exit when it is done executing. You can exit your program at any time, and return any exit code. If your output is correct, you will receive points regardless of the exit code; however, if your program is not accepted, the submission status will indicate "Invalid Return" (IR). Note that calling sys_exit does not flush the standard output stream, but instead immediately discards anything left in the buffer. This is not a problem when sys_exit is called indirectly by exit in libc, which always closes the standard streams before calling sys_exit.
  • sys_fcntl: Your programming language runtime might use this call. There is probably no use for it in the code you submit.
  • sys_fork: Not allowed. Programs may not sys_fork, and may be killed if they try, or the attempt will just fail anyway..
  • sys_getitimer: See sys_setitimer.
  • sys_getpid: This is often useful for seeding random number generators.
  • sys_kill: It's obviously not allowed to sys_kill other processes, and trying to do so will either cause your program to be sys_killed itself, or simply fail. Programs can sys_kill themselves though.
  • sys_lseek, sys_llseek: You can use this if you want, but be aware that it won't work if the standard streams turn out to be pipes instead of files.
  • sys_mmap: Analogous to sys_lseek.
  • sys_open: Not allowed. Your program will have standard input, standard output, and standard error streams already open when it is invoked. You are not allowed to attempt to read from or write to other files.
  • sys_read: This system call is necessary for reading input. Be aware however that the input may come from a pipe, in which case it won't be possible to sys_read large amounts of data in at once, but instead it will be necessary to check the return value to determine how many bytes were actually read, and then try again if there is yet more data to be read. This is not an issue when using I/O functions from programming language standard libraries (such as fread), as these will automatically call sys_read multiple times if necessary.
  • sys_setitimer: This might be useful for arranging for a recursive backtracking algorithm to automatically terminate when your program's time limit has nearly expired, so that your program can print out the best solution it's found so far and you can hope for the best.
  • sys_sigaction, sys_signal: Could be useful if used in conjunction with sys_setitimer. Note that attempts to use this system call to circumvent the grader are futile. For example, catching SIGXCPU might allow your program to execute for longer than the CPU time limit, but you'll get a TLE mark anyway, since the grader knows how much CPU time you used. (It also won't allow you to exceed the wall clock limit, since your program receives SIGKILL when this happens.)
  • sys_socketcall: Not allowed, since programs are not supposed to access the network. They will fail anyway since your program is not allowed to open new file descriptors.
  • sys_time: Like sys_getpid.
  • sys_write: Like sys_read.