|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
|
| Old News | Recommended Books | Minitutorial | Reference | Dubeau. | Beej | Rusling |
| Rochkind | Burkett et all | David Marshall | Linux Memory Management | Open Group Search Engine | Gnu C library | Etc |
System Calls -- slides
mail() {
struct rec { char name[20];
int number;
struct rec *next; }
struct rec *a; /* declare the pointer */
a = (struct rec *) malloc(sizeof(struct rec)); /* get the memory */
a-> = (struct rec *) malloc(sizeof(struct rec)); /*get another one*/
scanf("%s %d", a->, &(a->));
….
free(a); /* release the fist node and loose the second */
}
-->
mail( ) {
FILE *fp, *fopen(); char c;
system("ls -l > tmp"); /* invoking bourne shell */
fp = fopen("tmp", "r");
while ( (c = getc(fp)) != EOF)) putchar(c);
fclose(fp);
}
-->
main( ) {
FILE *infp, *outfp, *popen( ); int c;
infp = popen("ls -l", "r"); /* equivelant to ls -l | thisprogram */
outfp = popen("mail qli", "w");
while ( ( c = getc(infp)) != EOF) fprintf(outfp, "%c", c);
pclose(infp);
pclose(outfp);
}
-->
….
if ( (pid == fork( )) != 0 ) {
printf("I am the parent process\n");
… other stuff
wait(&status);
}
else {
printf("I am the child process\n");
…. other child process code
}
#include <.h>
main( ) {
….
signal(SIGINT, handler(SIGINT));
/* call hander() if SIGINT arrives */
kill -HUP pid Many system daemons catch this signal and reinitialize themselves.
…..
signal(SIGALRM, handletimeout());
…..
alarm(20);
….
void handletimeout() { …. }
#include <.h>
#include </signal.h>
#define TIMEOUT 10
static jmp_buf jbuf;
main(int argc, char argv[ ]) {
FILE *fp;
int ecode;
if ( ( fp = fopen( argv[1], "r") == NULL){
perror("ca't open file"); exit(1); }
while (1) {
if (setjmp(jbuf)) == TIMEOUT ) {
printf("Timeout\n"); ecode=0; break;}
else {
signal(SIGALRM, wentoff);
alarm(10);
printf(Enter ID number:");
if (scanf("%d%*c", &id) == EOF) {
ecode = 0; break; }
alarm(0); /* cancel alarm */
if (fseek(fp,id*sizeof(cust), 0)
==NULL) {
perror("can't seek");ecode=1;break;
} else if ( fread((char *) &cust,
sizeof(cust), 1, fp) ! = 1) {
perror("can't read");ecode=1;break;}
print_info(cust);
}} /* if, while */
} /* main() */
wentoff(int signal) {
printf("Timeout has occured\n");
longjmp(jbuf, TIMEOUT);
}
Program one.c :
char *argv[ ]={"two", "abc", "123", '\0'};
char *env[]={"X=100", "Y=200", '\0'};
mail() {
printf("Running one\n");
if (execve("/usr/home/qli/two", argv, env) <= 0 ) {
perror("Can't execl"); }
}
Program two.c :
main(argv, argv)
int argc; char *argv[]; {
char *getenv( ); int i;
printf("Running two\n");
for (I=0; I< I++) printf("%s\n", argv[i]);
printf("%s, %s, %s, %s\n", getenv("X") getenv("Y"), getenv("HOME"), getenv("PWD")); }
%two # running two from shell
Running two
two
(null), (null), /usr/home/qli, /usr/home/qli
%one # running one from shell
Running one
Running two
two
abc
123
100, 200, (null), (null)
#define Read 0
#define Write 1
#define STDIN 0
#define STDOUT 1
main ( ) {
FILE *fpt, *fpw;
int pfd[2]; /* pipe */
pipe(pfd);
if ((pid=fork()) != 0) { /* parent proc */
close(STDOUT);
dup(pfd([Write]);
close(pfd[Write]);
close(pfd[Read]);
….. some code, maybe execl();
} else { /* child process */
close(STDIN);
dup(pfd([Read]);
close(ptd[Read]);
close(pfd[Write]);
…. some code, maybe execl();
} /* if */
} /* main */
#include <.h>
#define ERROR -1
main() {
FILE *fdopen(), *fr, *fw;
int pid, pfd[2];
int i, n;
pipe(pfd);
if ((pid=fork()) == ERROR) {
printf("Can't fork\n"); exit(1); }
else if (pid != 0) { /* dad process */
fw = fdopen(pfd[1], "w");
for (i=1; i<=10; i++)
fprintf(fw, "%d\n", i);
fclose(fw);
exit(0);
} else
{ /* son process */
fr = fdopen(pfd[0], "r");
for (i=10; i? i++) {
fscanf(fr, "%d", &n);
printf("In son process: %d\n", n);
}
fclose(fr);
exit(0);
} /* if … else */
} /* main */
-->
[Aug 20, 2004] Manipulating Files And Directories In Unix Copyright (c) 1998-2002 by guy keren.
The following tutorial describes various common methods for reading and writing files and directories on a Unix system. Part of the information is common C knowledge, and is repeated here for completeness. Other information is Unix-specific, although DOS programmers will find some of it similar to what they saw in various DOS compilers. If you are a proficient C programmer, and know everything about the standard I/O functions, its buffering operations, and know functions such as
fseek()orfread(), you may skip the standard C library I/O functions section. If in doubt, at least skim through this section, to catch up on things you might not be familiar with, and at least look at the standard C library examples.This document is copyright (c) 1998-2002 by guy keren.
The material in this document is provided AS IS, without any expressed or implied warranty, or claim of fitness for a particular purpose. Neither the author nor any contributers shell be liable for any damages incured directly or indirectly by using the material contained in this document.
permission to copy this document (electronically or on paper, for personal or organization internal use) or publish it on-line is hereby granted, provided that the document is copied as-is, this copyright notice is preserved, and a link to the original document is written in the document's body, or in the page linking to the copy of this document.
Permission to make translations of this document is also granted, under these terms - assuming the translation preserves the meaning of the text, the copyright notice is preserved as-is, and a link to the original document is written in the document's body, or in the page linking to the copy of this document.
For any questions about the document and its license, please contact the author.
Advanced Programming in the UNIX(R) Environment
|
A rarity - a great computer
book., April 24, 2001
Preface 1. Introduction (a "whirlwind tour of Unix") 2. Unix Standardization and Implementations 3. File I/O 4. Files and Directories 5. Standard I/O Library 6. System Data Files and Information 7. The Environment of a Unix Process 8. Process Control 9. Process Relationships 10. Signals 11. Terminal I/O 12. Advanced I/O 13. Daemon Processes 14. Interprocess Communication 15. Advanced Interprocess Communication 16. A Database Library 17. Communicating with a PostScript Printer 18. A Modem Dialer 19. Pseudo Terminals Appendices A. Function Prototypes B. Miscellaneous Source Code (all source code is available for download) C. Solutions to Selected Exercises Bibliography Index The first thing to understand about the book is that while it can be used as just a reference work (the index is wonderful), it really is a book you can and should read. Even if you think you know a lot of this stuff, you can be surprised at what you can still learn. What makes the book so much more useful than just a collection of man-page print-outs (that dreary and painfully common form of UNIX "book") is the method of presentation. Stevens' basic atom of organization is the function call. For each call (or minor variations on a single call), he provides the C prototype, and then, in text, explains what the function does, what it's arguments are for, and then typically provides a small C program that demonstrates it in action, which he then explains. These function-level building blocks are arranged into related sets, each of which is a chapter in the book. Each chapter has a wrapper that consists of an introduction explaining some basic concepts and history of the functions described in that chapter, and some review exercises at the end. The chapters themselves are arranged so that the earlier chapters describe the basic functions, and the later chapters describe the more difficult functions. Every chapter both teaches the reader something of immediate use in writing code (even the introduction has sample programs), as well as preparing him for the more difficult subjects that lie ahead. Now for the caveats. Stevens absolutely assumes that you know how to program in C and that you know how to use Unix development tools (or at least that you have some other source from which to learn them). This is not the book to learn how to use C or particular shells, editors, compilers, linkers, or debuggers. Similarly, new Unix variants, such as Linux and MacOS X, receive no specific mention here at all (though the book is invaluable for both). Also, there is no discussion of the various GUI interfaces offered on many current Unix systems - for those, some other book will necessary. One other thing worth mentioning is the cost of the book. Don't be put off by it - Stevens' book has been justifying that cost for a lot of readers for a lot of years. In closing, I've been a developer for many years and have owned many computer books. I recommend very few of them, but can't recommend this one highly enough. It is one of the few books I've had that routinely lies open beside me when I work. In addition to my personal recommendation, you might look not only at all the positive reviews for this book, but also at the reviews for "competitive" books and notice how often they refer you back to this one. This book is the standard by which other UNIX programming books are measured, and so far, it has not been surpassed |
Using Unix System Calls by M. Rochkind, W.Toomey
Programming in C -- David Marshall 1994 Contains a lot of useful info on system calls
UNIX and C
Unix Seventh Edition Manual This is a classic. See vol1/man2.bun - system calls for historical information that may help to understand Unix kernel evolution.
Syscall specifications of Linux - Table of Contents by Louis-Dominique Dubeau.
[Beej's Guide] A fork() primer -- From Beej's Guide to Unix Interprocess Communication
[Dubeau] Syscall specifications of Linux - Table of Contents by Louis-Dominique DubSyscall specifications of Linux - Table of Contents by Louis-Dominique Dubeau.
[Beej's Guide] A fork() primer -- From Beej's Guide to Unix Interprocess Communication
Tour of the Linux kernel source
The Linux Kernel -- free book by David A Rusling. One of the best source of information about Linux kernel
Berkeley UNIX System Calls and Interprocess Communication
The Linux Programmer's Guide
, version 0.4 by B. Scott Burkett, Sven Goldt, John D. Harper, Sven van der Meer and Matt Welsh, is available in HTML, HTML (tared and gziped), LaTeX source, PDF and PostScript.http://www.opengroup.org/onlinepubs/7908799/
libc -- GNU C Library - GNU Project - Free Software Foundation (FSF)
Any Unix-like operating system needs a C library: the library which defines the ``system calls'' and other basic facilities such as malloc.
The history of Unix and various standards determine much of the interface of the C library. In general the GNU C library supports the ISO C and POSIX standards. We also try to support the features of popular Unix variants (including BSD and System V) when those do not conflict with the standards. Different compatibility modes (selectable when you compile an application) allow the peaceful coexistence of compatibility support for different varieties of Unix.
FreeCode Free Programming Source Code
UNIX Socket Programming in C -- Programming UNIX Sockets in C - Frequently Asked Questions Created by Vic Metcalfe, Andrew Gierth and other contributers May 21, 1998
W. Richard Stevens home page
Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.
Last modified: February 28, 2008