<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://so.v2.cs.unibo.it/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Pirata</id>
	<title>Sistemi Operativi - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://so.v2.cs.unibo.it/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Pirata"/>
	<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php/Special:Contributions/Pirata"/>
	<updated>2026-05-05T08:12:54Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.5</generator>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaTeorica_2012.07.16&amp;diff=515</id>
		<title>ProvaTeorica 2012.07.16</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaTeorica_2012.07.16&amp;diff=515"/>
		<updated>2014-03-28T16:54:11Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.cs.unibo.it/~renzo/so/compiti/2012-07-16.tot.pdf Link Testo]&lt;br /&gt;
&amp;lt;h2&amp;gt;Esercizio C.2&amp;lt;/h2&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mia Soluzione:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#define RED 0&lt;br /&gt;
#define BLUE 1&lt;br /&gt;
//restituisce 1 se c'e piu del 75% blue o 75% di red&lt;br /&gt;
//restituisce 0 se non c'e una maggiornza&lt;br /&gt;
int media(color){&lt;br /&gt;
	int mediared;&lt;br /&gt;
	int localinto =  into;&lt;br /&gt;
	if (color != NULL){&lt;br /&gt;
		localinto[color]++;&lt;br /&gt;
	}&lt;br /&gt;
	mediared = ((100*localinto[0])/(localinto[0]+localinto[1]));&lt;br /&gt;
	if(mediared &amp;gt;=75 || mediared &amp;lt; 25){&lt;br /&gt;
		return 1;//75% di rossi o di blue&lt;br /&gt;
	}else{&lt;br /&gt;
		return 0;// non c'e maggioranza		&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
monitor limcol{&lt;br /&gt;
&lt;br /&gt;
	conditon oktoenter[2];&lt;br /&gt;
	int into[2];&lt;br /&gt;
&lt;br /&gt;
	enter(color){&lt;br /&gt;
		if(media(color) != 1){// attendo perche' non c'e una maggioranza&lt;br /&gt;
			oktoenter[color].wait();&lt;br /&gt;
		}&lt;br /&gt;
		into[color]++;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	exit(color){&lt;br /&gt;
		into[color]--;&lt;br /&gt;
		if(media(1-color) == 1){&lt;br /&gt;
			oktoenter[1-color].signal();&lt;br /&gt;
		}else if(media(color) == 1){&lt;br /&gt;
			oktoenter[color].signal();&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
- Midolo&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#define ROSSO 0&lt;br /&gt;
#define BLU 1&lt;br /&gt;
&lt;br /&gt;
monitor limcol {&lt;br /&gt;
	condition oktoenter&lt;br /&gt;
	int running[2] // numero di processi rossi [0] e blu [1] in esecuzione&lt;br /&gt;
	queue waiting // coda dei colori dei processi in attesa&lt;br /&gt;
	&lt;br /&gt;
	/* restituisce true se aggiungendo un processo del colore passato viene rispettato il 75%&lt;br /&gt;
	dei processi di un colore */&lt;br /&gt;
	bool morethan75p(colore) { &lt;br /&gt;
		return (running[colore]+1&amp;gt;=running[1-colore]*3 || (running[colore]+1)*3&amp;lt;=running[1-colore])&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	// risveglia un processo del colore passato se è possibile farlo&lt;br /&gt;
	void checkwakeup() {&lt;br /&gt;
		if (waiting.empty() == false) {&lt;br /&gt;
			colore=waiting.head() // head resituisce l'elemento in testa senza rimuoverlo&lt;br /&gt;
			if (morethan75p(colore)) { &lt;br /&gt;
				waiting.dequeue()&lt;br /&gt;
				oktoenter.signal()&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	procedure entry enter(colore) {&lt;br /&gt;
		if (morethan75p(colore)==false) {&lt;br /&gt;
			waiting.enqueue(colore)&lt;br /&gt;
			oktoenter.wait()&lt;br /&gt;
		}&lt;br /&gt;
		running[colore]++&lt;br /&gt;
		checkwakeup()&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	procedure entry exit(colore) {&lt;br /&gt;
		running[colore]--&lt;br /&gt;
		checkwakeup()&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* la politica FIFO impedisce che si verifichi starvation, perché nel caso pessimo un processo&lt;br /&gt;
che vuole entrare dovrà attendere fino a che tutti quelli in esecuzione terminino */&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Daniele Cortesi&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
define RED 0&lt;br /&gt;
define BLUE 1&lt;br /&gt;
int majorcolor = -1&lt;br /&gt;
int numproc[2] = 0,0&lt;br /&gt;
cond oktoenter[2]&lt;br /&gt;
cond oktoexit[2]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
monitor limcol{&lt;br /&gt;
	procedure entry enter(COL){&lt;br /&gt;
		if(COL == majorcolor){&lt;br /&gt;
			numproc[COL]++&lt;br /&gt;
			if(((proc[majorcolor])*100/(numproc[COL] + numproc[1-COL] + 1)) &amp;gt;= 75)&lt;br /&gt;
				oktoenter[1-COL].signal()&lt;br /&gt;
		}&lt;br /&gt;
		else if(COL == 1-majorcolor){&lt;br /&gt;
			if((proc[majorcolor]*100/(numproc[COL] + numproc[1-COL] + 1)) &amp;lt; 75)&lt;br /&gt;
				oktoenter[COL].wait()&lt;br /&gt;
			if(majorcolor == -1)&lt;br /&gt;
				majorcolor == COL&lt;br /&gt;
			numproc[COL]++&lt;br /&gt;
		}&lt;br /&gt;
		else{&lt;br /&gt;
			numproc[COL]++&lt;br /&gt;
			majorcolor = COL&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	procedure entry exit(COL){&lt;br /&gt;
		if(numproc[COL] + numproc[1-COL] == 1){&lt;br /&gt;
			majorcolor = -1&lt;br /&gt;
			numproc[COL]--&lt;br /&gt;
			oktoenter[1-COL].signal()&lt;br /&gt;
		}&lt;br /&gt;
		else if(COL == majorcolor){&lt;br /&gt;
			if(((proc[majorcolor] - 1)*100/(numproc[COL] + numproc[1-COL] - 1)) &amp;lt; 75){&lt;br /&gt;
				oktoexit[COL].wait()&lt;br /&gt;
			numproc[COL]--&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		else if(COL == 1-majorcolor){&lt;br /&gt;
			numproc[COL]--&lt;br /&gt;
			if(((proc[majorcolor])*100/(numproc[COL] + numproc[1-COL] - 1)) &amp;gt;= 75)&lt;br /&gt;
				oktoexit[1-COL].signal()&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Fede&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
define RED 0&lt;br /&gt;
define BLU 1&lt;br /&gt;
&lt;br /&gt;
limcol{&lt;br /&gt;
	/*variable*/&lt;br /&gt;
	int array[2] = 0,0;&lt;br /&gt;
	color major = -1;&lt;br /&gt;
	/*condition*/&lt;br /&gt;
	condition oktoenter[2];&lt;br /&gt;
	condition oktoleave[2];&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	procedure entry enter( color ){&lt;br /&gt;
		if ( majorcolor == -1 ) &lt;br /&gt;
			majorcolor = color;&lt;br /&gt;
		if ( majorcolor == (1-color) &amp;amp;&amp;amp; newmajorcolor%(color) &amp;lt; 75% )&lt;br /&gt;
			oktoenter[color].wait();&lt;br /&gt;
		array[color]++; &lt;br /&gt;
		if ( majorcolor == -1 )            //se un processo si risveglia nel monitor vuoto deve impostare majorcolor&lt;br /&gt;
			majorcolor = color;&lt;br /&gt;
		if ( newmajorcolor%(1-color) &amp;gt;= 75% )         &lt;br /&gt;
			oktoenter[1-color].signal();&lt;br /&gt;
					}&lt;br /&gt;
&lt;br /&gt;
	procedure entry exit( color ){&lt;br /&gt;
		if ( majorcolor == color &amp;amp;&amp;amp; majorcolor_less1%(color) &amp;gt; 75% )&lt;br /&gt;
			oktoleave[color].wait();&lt;br /&gt;
		array[color]--;&lt;br /&gt;
		if ( !colorsempty() )&lt;br /&gt;
			if ( majorcolor_less1%(color) &amp;gt;= 75% )&lt;br /&gt;
				oktoleave[1-color].signal();&lt;br /&gt;
		else{&lt;br /&gt;
			majorcolor == -1;&lt;br /&gt;
			oktoenter[1-color].signal();&lt;br /&gt;
				}&lt;br /&gt;
					}&lt;br /&gt;
		 &lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
newmajorcolor%(color) calcola la nuova % del colore maggiore aggiungendo color&lt;br /&gt;
majorcolor_less1%(color) calcola la nuova % del colore maggiore togliendo color&lt;br /&gt;
*/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Pir@t@&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2014.01.23&amp;diff=491</id>
		<title>ProvaPratica 2014.01.23</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2014.01.23&amp;diff=491"/>
		<updated>2014-03-23T14:07:08Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[C esercizi 1 e 2]&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
*Esercizio 1: Linguaggio C (obbligatorio): (20 punti)&lt;br /&gt;
*Scrivere un programma in C “linean” che prenda come parametro il pathname di un file e un numero intero (che chiameremo n). &lt;br /&gt;
*Il programma deve stampare come output il numero di caratteri presenti nella n-ma riga del file se il file e' un file regolare di testo, non deve stampare nulla negli altri casi.&lt;br /&gt;
*Un file viene considerato di testo se tutti i suoi byte hanno valori compresi nel range 1-127.&lt;br /&gt;
*Per controllare se il file e' “regolare” usare la system call lstat&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 200&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int istextfile( FILE *fd ){                                   //check if the file is a regular file of text,it means that every byte has a value between 1-127&lt;br /&gt;
	int c;&lt;br /&gt;
	while ( (c = getc ( fd ) ) != EOF &amp;amp;&amp;amp; c &amp;lt;= 127 );&lt;br /&gt;
	rewind(fd);                                           //sets the file's cursor at the beginning,otherwise the linean program cannot find the line&lt;br /&gt;
	return ( (c == EOF)? 1 : 0 );&lt;br /&gt;
}&lt;br /&gt;
	 &lt;br /&gt;
		&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void linean( char *path , int line ){&lt;br /&gt;
	FILE *fd;&lt;br /&gt;
	struct stat buf;&lt;br /&gt;
	char bufline[BUFFSIZE];&lt;br /&gt;
	char *res;&lt;br /&gt;
	int n = line;&lt;br /&gt;
	res = NULL;&lt;br /&gt;
	lstat( path , &amp;amp;buf );&lt;br /&gt;
	if ( S_ISREG( buf.st_mode ) )&lt;br /&gt;
	{&lt;br /&gt;
		fd = fopen( path , &amp;quot;r&amp;quot; );&lt;br /&gt;
		if ( fd == NULL )&lt;br /&gt;
		{&lt;br /&gt;
			perror( &amp;quot;Error to open the file\n&amp;quot; );&lt;br /&gt;
			exit(1);&lt;br /&gt;
		}&lt;br /&gt;
			if ( istextfile( fd ) )&lt;br /&gt;
			{&lt;br /&gt;
				while ( ( line &amp;gt; 0 ) )                                        //iterates until at the nline and when is reached then checks if the buffer contains something&lt;br /&gt;
				{&lt;br /&gt;
					res = fgets(bufline, BUFFSIZE , fd );    //every time the nline is buffered,when exits from the cicle, the buffer contains the nline &lt;br /&gt;
					line--;&lt;br /&gt;
				}&lt;br /&gt;
				if ( res != NULL )                &lt;br /&gt;
				{&lt;br /&gt;
					printf( &amp;quot;The file %s in line %d has : %d char\n&amp;quot; , path , n , ( strlen( bufline ) - 1 )  );  //strlen also consider the carriage return&lt;br /&gt;
				}&lt;br /&gt;
				else&lt;br /&gt;
				printf( &amp;quot;Error: isn't possible to find the line\n&amp;quot; );&lt;br /&gt;
			}&lt;br /&gt;
			else&lt;br /&gt;
				printf( &amp;quot;Error is not a regular file of text\n&amp;quot; );&lt;br /&gt;
				&lt;br /&gt;
		fclose(fd);&lt;br /&gt;
				&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
		printf( &amp;quot;The file %s isn't a regular file\n&amp;quot; , path );&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int n = atoi( argv[2] );&lt;br /&gt;
	linean( argv[1] , n );&lt;br /&gt;
	return(0);&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
*Esercizio 2: completamento (10 punti)&lt;br /&gt;
*Si scriva un programma C chiamato “lineandir”. Il risultato del programma, stampato su standard output, deve essere un solo&lt;br /&gt;
*numero intero: la somma del numero di caratteri presenti nelle n-me righe di tutti i file regolari, di testo, non nascosti (il primo&lt;br /&gt;
*carattere deve essere diverso da punto) della directory corrente. &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 200&lt;br /&gt;
&lt;br /&gt;
int istextfile( FILE *fd );&lt;br /&gt;
int linean( char *path , int line );&lt;br /&gt;
void lineandir( char *dir , int line );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void lineandir( char *dir , int line ){&lt;br /&gt;
	int count , i;&lt;br /&gt;
	int sumline;&lt;br /&gt;
	struct dirent **de;&lt;br /&gt;
	sumline = 0;&lt;br /&gt;
	count = scandir( dir , &amp;amp;de , NULL , alphasort );&lt;br /&gt;
	for ( i = 0 ; i &amp;lt; count ; i++ )&lt;br /&gt;
	{&lt;br /&gt;
		if ( de[i]-&amp;gt;d_name[0] != '.' )     //doesn't consider the hidden files&lt;br /&gt;
			sumline = sumline + linean( de[i]-&amp;gt;d_name , line );&lt;br /&gt;
		free(de[i]);&lt;br /&gt;
	}&lt;br /&gt;
	printf(&amp;quot;The sum of char for every regular text file's nline in the current directory is: %d\n&amp;quot; , sumline );&lt;br /&gt;
	free(de);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int istextfile( FILE *fd ){                                  &lt;br /&gt;
	int c;&lt;br /&gt;
	while ( (c = getc ( fd ) ) != EOF &amp;amp;&amp;amp; c &amp;lt;= 127 );&lt;br /&gt;
	rewind(fd);                                           &lt;br /&gt;
	return ( (c == EOF)? 1 : 0 );&lt;br /&gt;
}&lt;br /&gt;
	 &lt;br /&gt;
		&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*linean is modified so it can return the value of the char in the nline */&lt;br /&gt;
int linean( char *path , int line ){&lt;br /&gt;
	FILE *fd;&lt;br /&gt;
	struct stat buf;&lt;br /&gt;
	char bufline[BUFFSIZE];&lt;br /&gt;
	char *res;&lt;br /&gt;
	int n = line;&lt;br /&gt;
	res = NULL;&lt;br /&gt;
	lstat( path , &amp;amp;buf );&lt;br /&gt;
	if ( S_ISREG( buf.st_mode ) )&lt;br /&gt;
	{&lt;br /&gt;
		fd = fopen( path , &amp;quot;r&amp;quot; );&lt;br /&gt;
		if ( fd == NULL )&lt;br /&gt;
		{&lt;br /&gt;
			perror( &amp;quot;Error to open the file\n&amp;quot; );&lt;br /&gt;
			exit(1);&lt;br /&gt;
		}&lt;br /&gt;
			if ( istextfile( fd ) )&lt;br /&gt;
			{&lt;br /&gt;
				while ( ( line &amp;gt; 0 ) )                                        &lt;br /&gt;
				{&lt;br /&gt;
					res = fgets(bufline, BUFFSIZE , fd );    &lt;br /&gt;
					line--;&lt;br /&gt;
				}&lt;br /&gt;
				if ( res != NULL )                &lt;br /&gt;
				{&lt;br /&gt;
					return( ( strlen( bufline ) - 1 )  );  &lt;br /&gt;
				}&lt;br /&gt;
				else&lt;br /&gt;
				printf( &amp;quot;Error: isn't possible to find the line\n&amp;quot; );&lt;br /&gt;
			}&lt;br /&gt;
			else&lt;br /&gt;
				printf( &amp;quot;Error is not a regular file of text\n&amp;quot; );&lt;br /&gt;
				&lt;br /&gt;
		fclose(fd);&lt;br /&gt;
				&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
		printf( &amp;quot;The file %s isn't a regular file\n&amp;quot; , path );&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int n = atoi( argv[1] );&lt;br /&gt;
	lineandir( &amp;quot;./&amp;quot; , n );&lt;br /&gt;
	return(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[Bash esercizio 3]&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Bash&amp;quot;&amp;gt;&lt;br /&gt;
#Esercizio 3: Script bash o Python: (10 punti):&lt;br /&gt;
#Il comando che dovrete implementare come script shell o programma python e' updatedir. Updatedir prende due directorycome parametri.&lt;br /&gt;
#updatedir dira dirb deve copiare in dirb tutti i file regolari che sono in dira e non in dirb. Se un file regolare e' presente con lo stesso nome sia in&lt;br /&gt;
#dira sia in dirb, il file deve essere copiato dalla dira alla dirb solo se i contenuti differiscono.&lt;br /&gt;
&lt;br /&gt;
#! /bin/bash&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
for file in &amp;quot;$1&amp;quot;/*; do&lt;br /&gt;
	if [[ -f $file ]] ; then          #checks if the file is regular&lt;br /&gt;
		thereisnotfile=1          #variable that identify if the file is also in dir2&lt;br /&gt;
		for file2 in &amp;quot;$2&amp;quot;/*; do&lt;br /&gt;
			if [[ -f $file2 ]] ; then&lt;br /&gt;
				if [[  $file2 !=  $file ]] ; then      #checks if the file is only in dir1 and not in dir2&lt;br /&gt;
					continue&lt;br /&gt;
				else&lt;br /&gt;
					thereisnotfile=0            #the file is in both dir&lt;br /&gt;
				fi&lt;br /&gt;
				if [[ $thereisnotfile -eq 0 ]]; then      &lt;br /&gt;
					if [[ $(md5sum &amp;quot;$file&amp;quot;) != $(md5sum &amp;quot;$file2&amp;quot;) ]] ; then   #checks if the file in dir1 is different of file in dir2 by md5sum &lt;br /&gt;
						bn=$(basename &amp;quot;$file&amp;quot;)&lt;br /&gt;
						cp $file  &amp;quot;$2&amp;quot;/&amp;quot;$bn&amp;quot;&lt;br /&gt;
						thereisnotfile=2        #is set in this way for not continue to iterate&lt;br /&gt;
					fi&lt;br /&gt;
				fi&lt;br /&gt;
			fi&lt;br /&gt;
		done&lt;br /&gt;
		if [[ $thereisnotfile -eq 1 ]]; then&lt;br /&gt;
			bn=$(basename &amp;quot;$file&amp;quot;)&lt;br /&gt;
			cp $file  &amp;quot;$2&amp;quot;/&amp;quot;$bn&amp;quot;&lt;br /&gt;
		fi&lt;br /&gt;
	fi&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;		&lt;br /&gt;
Pirata&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2014.01.23&amp;diff=490</id>
		<title>ProvaPratica 2014.01.23</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2014.01.23&amp;diff=490"/>
		<updated>2014-03-23T14:06:10Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[C esercizi 1 e 2]&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
*Esercizio 1: Linguaggio C (obbligatorio): (20 punti)&lt;br /&gt;
*Scrivere un programma in C “linean” che prenda come parametro il pathname di un file e un numero intero (che chiameremo n). &lt;br /&gt;
*Il programma deve stampare come output il numero di caratteri presenti nella n-ma riga del file se il file e' un file regolare di testo, non deve stampare nulla negli altri casi.&lt;br /&gt;
*Un file viene considerato di testo se tutti i suoi byte hanno valori compresi nel range 1-127.&lt;br /&gt;
*Per controllare se il file e' “regolare” usare la system call lstat&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 200&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int istextfile( FILE *fd ){                                   //check if the file is a regular file of text,it means that every byte has a value between 1-127&lt;br /&gt;
	int c;&lt;br /&gt;
	while ( (c = getc ( fd ) ) != EOF &amp;amp;&amp;amp; c &amp;lt;= 127 );&lt;br /&gt;
	rewind(fd);                                           //sets the file's cursor at the beginning,otherwise the linean program cannot find the line&lt;br /&gt;
	return ( (c == EOF)? 1 : 0 );&lt;br /&gt;
}&lt;br /&gt;
	 &lt;br /&gt;
		&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void linean( char *path , int line ){&lt;br /&gt;
	FILE *fd;&lt;br /&gt;
	struct stat buf;&lt;br /&gt;
	char bufline[BUFFSIZE];&lt;br /&gt;
	char *res;&lt;br /&gt;
	int n = line;&lt;br /&gt;
	res = NULL;&lt;br /&gt;
	lstat( path , &amp;amp;buf );&lt;br /&gt;
	if ( S_ISREG( buf.st_mode ) )&lt;br /&gt;
	{&lt;br /&gt;
		fd = fopen( path , &amp;quot;r&amp;quot; );&lt;br /&gt;
		if ( fd == NULL )&lt;br /&gt;
		{&lt;br /&gt;
			perror( &amp;quot;Error to open the file\n&amp;quot; );&lt;br /&gt;
			exit(1);&lt;br /&gt;
		}&lt;br /&gt;
			if ( istextfile( fd ) )&lt;br /&gt;
			{&lt;br /&gt;
				while ( ( line &amp;gt; 0 ) )                                        //iterates until at the nline and when is reached then checks if the buffer contains something&lt;br /&gt;
				{&lt;br /&gt;
					res = fgets(bufline, BUFFSIZE , fd );    //every time the nline is buffered,when exits from the cicle, the buffer contains the nline &lt;br /&gt;
					line--;&lt;br /&gt;
				}&lt;br /&gt;
				if ( res != NULL )                &lt;br /&gt;
				{&lt;br /&gt;
					printf( &amp;quot;The file %s in line %d has : %d char\n&amp;quot; , path , n , ( strlen( bufline ) - 1 )  );  //strlen also consider the carriage return&lt;br /&gt;
				}&lt;br /&gt;
				else&lt;br /&gt;
				printf( &amp;quot;Error: isn't possible to find the line\n&amp;quot; );&lt;br /&gt;
			}&lt;br /&gt;
			else&lt;br /&gt;
				printf( &amp;quot;Error is not a regular file of text\n&amp;quot; );&lt;br /&gt;
				&lt;br /&gt;
		fclose(fd);&lt;br /&gt;
				&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
		printf( &amp;quot;The file %s isn't a regular file\n&amp;quot; , path );&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int n = atoi( argv[2] );&lt;br /&gt;
	linean( argv[1] , n );&lt;br /&gt;
	return(0);&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
*Esercizio 2: completamento (10 punti)&lt;br /&gt;
*Si scriva un programma C chiamato “lineandir”. Il risultato del programma, stampato su standard output, deve essere un solo&lt;br /&gt;
*numero intero: la somma del numero di caratteri presenti nelle n-me righe di tutti i file regolari, di testo, non nascosti (il primo&lt;br /&gt;
*carattere deve essere diverso da punto) della directory corrente. &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 200&lt;br /&gt;
&lt;br /&gt;
int istextfile( FILE *fd );&lt;br /&gt;
int linean( char *path , int line );&lt;br /&gt;
void lineandir( char *dir , int line );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void lineandir( char *dir , int line ){&lt;br /&gt;
	int count , i;&lt;br /&gt;
	int sumline;&lt;br /&gt;
	struct dirent **de;&lt;br /&gt;
	sumline = 0;&lt;br /&gt;
	count = scandir( dir , &amp;amp;de , NULL , alphasort );&lt;br /&gt;
	for ( i = 0 ; i &amp;lt; count ; i++ )&lt;br /&gt;
	{&lt;br /&gt;
		if ( de[i]-&amp;gt;d_name[0] != '.' )     //doesn't consider the hidden files&lt;br /&gt;
			sumline = sumline + linean( de[i]-&amp;gt;d_name , line );&lt;br /&gt;
		free(de[i]);&lt;br /&gt;
	}&lt;br /&gt;
	printf(&amp;quot;The sum of char for every regular text file's nline in the current directory is: %d\n&amp;quot; , sumline );&lt;br /&gt;
	free(de);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int istextfile( FILE *fd ){                                  &lt;br /&gt;
	int c;&lt;br /&gt;
	while ( (c = getc ( fd ) ) != EOF &amp;amp;&amp;amp; c &amp;lt;= 127 );&lt;br /&gt;
	rewind(fd);                                           &lt;br /&gt;
	return ( (c == EOF)? 1 : 0 );&lt;br /&gt;
}&lt;br /&gt;
	 &lt;br /&gt;
		&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*linean is modified so it can return the value of the char in the nline */&lt;br /&gt;
int linean( char *path , int line ){&lt;br /&gt;
	FILE *fd;&lt;br /&gt;
	struct stat buf;&lt;br /&gt;
	char bufline[BUFFSIZE];&lt;br /&gt;
	char *res;&lt;br /&gt;
	int n = line;&lt;br /&gt;
	res = NULL;&lt;br /&gt;
	lstat( path , &amp;amp;buf );&lt;br /&gt;
	if ( S_ISREG( buf.st_mode ) )&lt;br /&gt;
	{&lt;br /&gt;
		fd = fopen( path , &amp;quot;r&amp;quot; );&lt;br /&gt;
		if ( fd == NULL )&lt;br /&gt;
		{&lt;br /&gt;
			perror( &amp;quot;Error to open the file\n&amp;quot; );&lt;br /&gt;
			exit(1);&lt;br /&gt;
		}&lt;br /&gt;
			if ( istextfile( fd ) )&lt;br /&gt;
			{&lt;br /&gt;
				while ( ( line &amp;gt; 0 ) )                                        &lt;br /&gt;
				{&lt;br /&gt;
					res = fgets(bufline, BUFFSIZE , fd );    &lt;br /&gt;
					line--;&lt;br /&gt;
				}&lt;br /&gt;
				if ( res != NULL )                &lt;br /&gt;
				{&lt;br /&gt;
					return( ( strlen( bufline ) - 1 )  );  &lt;br /&gt;
				}&lt;br /&gt;
				else&lt;br /&gt;
				printf( &amp;quot;Error: isn't possible to find the line\n&amp;quot; );&lt;br /&gt;
			}&lt;br /&gt;
			else&lt;br /&gt;
				printf( &amp;quot;Error is not a regular file of text\n&amp;quot; );&lt;br /&gt;
				&lt;br /&gt;
		fclose(fd);&lt;br /&gt;
				&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
		printf( &amp;quot;The file %s isn't a regular file\n&amp;quot; , path );&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int n = atoi( argv[1] );&lt;br /&gt;
	lineandir( &amp;quot;./&amp;quot; , n );&lt;br /&gt;
	return(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Bash&amp;quot;&amp;gt;&lt;br /&gt;
[Bash esercizio 3]&lt;br /&gt;
#Esercizio 3: Script bash o Python: (10 punti):&lt;br /&gt;
#Il comando che dovrete implementare come script shell o programma python e' updatedir. Updatedir prende due directorycome parametri.&lt;br /&gt;
#updatedir dira dirb deve copiare in dirb tutti i file regolari che sono in dira e non in dirb. Se un file regolare e' presente con lo stesso nome sia in&lt;br /&gt;
#dira sia in dirb, il file deve essere copiato dalla dira alla dirb solo se i contenuti differiscono.&lt;br /&gt;
&lt;br /&gt;
#! /bin/bash&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
for file in &amp;quot;$1&amp;quot;/*; do&lt;br /&gt;
	if [[ -f $file ]] ; then          #checks if the file is regular&lt;br /&gt;
		thereisnotfile=1          #variable that identify if the file is also in dir2&lt;br /&gt;
		for file2 in &amp;quot;$2&amp;quot;/*; do&lt;br /&gt;
			if [[ -f $file2 ]] ; then&lt;br /&gt;
				if [[  $file2 !=  $file ]] ; then      #checks if the file is only in dir1 and not in dir2&lt;br /&gt;
					continue&lt;br /&gt;
				else&lt;br /&gt;
					thereisnotfile=0            #the file is in both dir&lt;br /&gt;
				fi&lt;br /&gt;
				if [[ $thereisnotfile -eq 0 ]]; then      &lt;br /&gt;
					if [[ $(md5sum &amp;quot;$file&amp;quot;) != $(md5sum &amp;quot;$file2&amp;quot;) ]] ; then   #checks if the file in dir1 is different of file in dir2 by md5sum &lt;br /&gt;
						bn=$(basename &amp;quot;$file&amp;quot;)&lt;br /&gt;
						cp $file  &amp;quot;$2&amp;quot;/&amp;quot;$bn&amp;quot;&lt;br /&gt;
						thereisnotfile=2        #is set in this way for not continue to iterate&lt;br /&gt;
					fi&lt;br /&gt;
				fi&lt;br /&gt;
			fi&lt;br /&gt;
		done&lt;br /&gt;
		if [[ $thereisnotfile -eq 1 ]]; then&lt;br /&gt;
			bn=$(basename &amp;quot;$file&amp;quot;)&lt;br /&gt;
			cp $file  &amp;quot;$2&amp;quot;/&amp;quot;$bn&amp;quot;&lt;br /&gt;
		fi&lt;br /&gt;
	fi&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;		&lt;br /&gt;
Pirata&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2014.01.23&amp;diff=489</id>
		<title>ProvaPratica 2014.01.23</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2014.01.23&amp;diff=489"/>
		<updated>2014-03-23T13:58:02Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[C esercizi 1 e 2]&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
*Esercizio 1: Linguaggio C (obbligatorio): (20 punti)&lt;br /&gt;
*Scrivere un programma in C “linean” che prenda come parametro il pathname di un file e un numero intero (che chiameremo n). &lt;br /&gt;
*Il programma deve stampare come output il numero di caratteri presenti nella n-ma riga del file se il file e' un file regolare di testo, non deve stampare nulla negli altri casi.&lt;br /&gt;
*Un file viene considerato di testo se tutti i suoi byte hanno valori compresi nel range 1-127.&lt;br /&gt;
*Per controllare se il file e' “regolare” usare la system call lstat&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 200&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int istextfile( FILE *fd ){                                   //check if the file is a regular file of text,it means that every byte has a value between 1-127&lt;br /&gt;
	int c;&lt;br /&gt;
	while ( (c = getc ( fd ) ) != EOF &amp;amp;&amp;amp; c &amp;lt;= 127 );&lt;br /&gt;
	rewind(fd);                                           //sets the file's cursor at the beginning,otherwise the linean program cannot find the line&lt;br /&gt;
	return ( (c == EOF)? 1 : 0 );&lt;br /&gt;
}&lt;br /&gt;
	 &lt;br /&gt;
		&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void linean( char *path , int line ){&lt;br /&gt;
	FILE *fd;&lt;br /&gt;
	struct stat buf;&lt;br /&gt;
	char bufline[BUFFSIZE];&lt;br /&gt;
	char *res;&lt;br /&gt;
	int n = line;&lt;br /&gt;
	res = NULL;&lt;br /&gt;
	lstat( path , &amp;amp;buf );&lt;br /&gt;
	if ( S_ISREG( buf.st_mode ) )&lt;br /&gt;
	{&lt;br /&gt;
		fd = fopen( path , &amp;quot;r&amp;quot; );&lt;br /&gt;
		if ( fd == NULL )&lt;br /&gt;
		{&lt;br /&gt;
			perror( &amp;quot;Error to open the file\n&amp;quot; );&lt;br /&gt;
			exit(1);&lt;br /&gt;
		}&lt;br /&gt;
			if ( istextfile( fd ) )&lt;br /&gt;
			{&lt;br /&gt;
				while ( ( line &amp;gt; 0 ) )                                        //iterates until at the nline and when is reached then checks if the buffer contains something&lt;br /&gt;
				{&lt;br /&gt;
					res = fgets(bufline, BUFFSIZE , fd );    //every time the nline is buffered,when exits from the cicle, the buffer contains the nline &lt;br /&gt;
					line--;&lt;br /&gt;
				}&lt;br /&gt;
				if ( res != NULL )                &lt;br /&gt;
				{&lt;br /&gt;
					printf( &amp;quot;The file %s in line %d has : %d char\n&amp;quot; , path , n , ( strlen( bufline ) - 1 )  );  //strlen also consider the carriage return&lt;br /&gt;
				}&lt;br /&gt;
				else&lt;br /&gt;
				printf( &amp;quot;Error: isn't possible to find the line\n&amp;quot; );&lt;br /&gt;
			}&lt;br /&gt;
			else&lt;br /&gt;
				printf( &amp;quot;Error is not a regular file of text\n&amp;quot; );&lt;br /&gt;
				&lt;br /&gt;
		fclose(fd);&lt;br /&gt;
				&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
		printf( &amp;quot;The file %s isn't a regular file\n&amp;quot; , path );&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int n = atoi( argv[2] );&lt;br /&gt;
	linean( argv[1] , n );&lt;br /&gt;
	return(0);&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
*Esercizio 2: completamento (10 punti)&lt;br /&gt;
*Si scriva un programma C chiamato “lineandir”. Il risultato del programma, stampato su standard output, deve essere un solo&lt;br /&gt;
*numero intero: la somma del numero di caratteri presenti nelle n-me righe di tutti i file regolari, di testo, non nascosti (il primo&lt;br /&gt;
*carattere deve essere diverso da punto) della directory corrente. &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 200&lt;br /&gt;
&lt;br /&gt;
int istextfile( FILE *fd );&lt;br /&gt;
int linean( char *path , int line );&lt;br /&gt;
void lineandir( char *dir , int line );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void lineandir( char *dir , int line ){&lt;br /&gt;
	int count , i;&lt;br /&gt;
	int sumline;&lt;br /&gt;
	struct dirent **de;&lt;br /&gt;
	sumline = 0;&lt;br /&gt;
	count = scandir( dir , &amp;amp;de , NULL , alphasort );&lt;br /&gt;
	for ( i = 0 ; i &amp;lt; count ; i++ )&lt;br /&gt;
	{&lt;br /&gt;
		if ( de[i]-&amp;gt;d_name[0] != '.' )     //doesn't consider the hidden files&lt;br /&gt;
			sumline = sumline + linean( de[i]-&amp;gt;d_name , line );&lt;br /&gt;
		free(de[i]);&lt;br /&gt;
	}&lt;br /&gt;
	printf(&amp;quot;The sum of char for every regular text file's nline in the current directory is: %d\n&amp;quot; , sumline );&lt;br /&gt;
	free(de);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int istextfile( FILE *fd ){                                  &lt;br /&gt;
	int c;&lt;br /&gt;
	while ( (c = getc ( fd ) ) != EOF &amp;amp;&amp;amp; c &amp;lt;= 127 );&lt;br /&gt;
	rewind(fd);                                           &lt;br /&gt;
	return ( (c == EOF)? 1 : 0 );&lt;br /&gt;
}&lt;br /&gt;
	 &lt;br /&gt;
		&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*linean is modified so it can return the value of the char in the nline */&lt;br /&gt;
int linean( char *path , int line ){&lt;br /&gt;
	FILE *fd;&lt;br /&gt;
	struct stat buf;&lt;br /&gt;
	char bufline[BUFFSIZE];&lt;br /&gt;
	char *res;&lt;br /&gt;
	int n = line;&lt;br /&gt;
	res = NULL;&lt;br /&gt;
	lstat( path , &amp;amp;buf );&lt;br /&gt;
	if ( S_ISREG( buf.st_mode ) )&lt;br /&gt;
	{&lt;br /&gt;
		fd = fopen( path , &amp;quot;r&amp;quot; );&lt;br /&gt;
		if ( fd == NULL )&lt;br /&gt;
		{&lt;br /&gt;
			perror( &amp;quot;Error to open the file\n&amp;quot; );&lt;br /&gt;
			exit(1);&lt;br /&gt;
		}&lt;br /&gt;
			if ( istextfile( fd ) )&lt;br /&gt;
			{&lt;br /&gt;
				while ( ( line &amp;gt; 0 ) )                                        &lt;br /&gt;
				{&lt;br /&gt;
					res = fgets(bufline, BUFFSIZE , fd );    &lt;br /&gt;
					line--;&lt;br /&gt;
				}&lt;br /&gt;
				if ( res != NULL )                &lt;br /&gt;
				{&lt;br /&gt;
					return( ( strlen( bufline ) - 1 )  );  &lt;br /&gt;
				}&lt;br /&gt;
				else&lt;br /&gt;
				printf( &amp;quot;Error: isn't possible to find the line\n&amp;quot; );&lt;br /&gt;
			}&lt;br /&gt;
			else&lt;br /&gt;
				printf( &amp;quot;Error is not a regular file of text\n&amp;quot; );&lt;br /&gt;
				&lt;br /&gt;
		fclose(fd);&lt;br /&gt;
				&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
		printf( &amp;quot;The file %s isn't a regular file\n&amp;quot; , path );&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int n = atoi( argv[1] );&lt;br /&gt;
	lineandir( &amp;quot;./&amp;quot; , n );&lt;br /&gt;
	return(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;	&lt;br /&gt;
Pirata&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2014.01.23&amp;diff=488</id>
		<title>ProvaPratica 2014.01.23</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2014.01.23&amp;diff=488"/>
		<updated>2014-03-23T13:50:34Z</updated>

		<summary type="html">&lt;p&gt;Pirata: Created page with &amp;quot;[C esercizi 1 e 2] &amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt; /* *Esercizio 1: Linguaggio C (obbligatorio): (20 punti) *Scrivere un programma in C “linean” che prenda come parametro il pa...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[C esercizi 1 e 2]&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
*Esercizio 1: Linguaggio C (obbligatorio): (20 punti)&lt;br /&gt;
*Scrivere un programma in C “linean” che prenda come parametro il pathname di un file e un numero intero (che chiameremo n). &lt;br /&gt;
*Il programma deve stampare come output il numero di caratteri presenti nella n-ma riga del file se il file e' un file regolare di testo, non deve stampare nulla negli altri casi.&lt;br /&gt;
*Un file viene considerato di testo se tutti i suoi byte hanno valori compresi nel range 1-127.&lt;br /&gt;
*Per controllare se il file e' “regolare” usare la system call lstat&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 200&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int istextfile( FILE *fd ){                                   //check if the file is a regular file of text,it means that every byte has a value between 1-127&lt;br /&gt;
	int c;&lt;br /&gt;
	while ( (c = getc ( fd ) ) != EOF &amp;amp;&amp;amp; c &amp;lt;= 127 );&lt;br /&gt;
	rewind(fd);                                           //sets the file's cursor at the beginning,otherwise the linean program cannot find the line&lt;br /&gt;
	return ( (c == EOF)? 1 : 0 );&lt;br /&gt;
}&lt;br /&gt;
	 &lt;br /&gt;
		&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void linean( char *path , int line ){&lt;br /&gt;
	FILE *fd;&lt;br /&gt;
	struct stat buf;&lt;br /&gt;
	char bufline[BUFFSIZE];&lt;br /&gt;
	char *res;&lt;br /&gt;
	int n = line;&lt;br /&gt;
	res = NULL;&lt;br /&gt;
	lstat( path , &amp;amp;buf );&lt;br /&gt;
	if ( S_ISREG( buf.st_mode ) )&lt;br /&gt;
	{&lt;br /&gt;
		fd = fopen( path , &amp;quot;r&amp;quot; );&lt;br /&gt;
		if ( fd == NULL )&lt;br /&gt;
		{&lt;br /&gt;
			perror( &amp;quot;Error to open the file\n&amp;quot; );&lt;br /&gt;
			exit(1);&lt;br /&gt;
		}&lt;br /&gt;
			if ( istextfile( fd ) )&lt;br /&gt;
			{&lt;br /&gt;
				while ( ( line &amp;gt; 0 ) )                                        //iterates until at the nline and when is reached then checks if the buffer contains something&lt;br /&gt;
				{&lt;br /&gt;
					res = fgets(bufline, BUFFSIZE , fd );    //every time the nline is buffered,when exits from the cicle, the buffer contains the nline &lt;br /&gt;
					line--;&lt;br /&gt;
				}&lt;br /&gt;
				if ( res != NULL )                &lt;br /&gt;
				{&lt;br /&gt;
					printf( &amp;quot;The file %s in line %d has : %d char\n&amp;quot; , path , n , ( strlen( bufline ) - 1 )  );  //strlen also consider the carriage return&lt;br /&gt;
				}&lt;br /&gt;
				else&lt;br /&gt;
				printf( &amp;quot;Error: isn't possible to find the line\n&amp;quot; );&lt;br /&gt;
			}&lt;br /&gt;
			else&lt;br /&gt;
				printf( &amp;quot;Error is not a regular file of text\n&amp;quot; );&lt;br /&gt;
				&lt;br /&gt;
		fclose(fd);&lt;br /&gt;
				&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
		printf( &amp;quot;The file %s isn't a regular file\n&amp;quot; , path );&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
	&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int n = atoi( argv[2] );&lt;br /&gt;
	linean( argv[1] , n );&lt;br /&gt;
	return(0);&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Pirata&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Main_Page&amp;diff=487</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Main_Page&amp;diff=487"/>
		<updated>2014-03-23T13:46:07Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Questo &amp;amp;egrave; il Wiki del Corso di Sistemi Operativi&lt;br /&gt;
&lt;br /&gt;
[[Esercizi a caso del Prof.]]&lt;br /&gt;
&lt;br /&gt;
[[Decalogo di Programmazione Concorrente]]&lt;br /&gt;
&lt;br /&gt;
[[Python Programma tieni punteggio.]]&lt;br /&gt;
&lt;br /&gt;
[[Comandi visti alle lezioni.]]&lt;br /&gt;
&lt;br /&gt;
[[SYS CALL viste a lezione.]]&lt;br /&gt;
&lt;br /&gt;
[[Parametri con getopt().]]&lt;br /&gt;
&lt;br /&gt;
[[Funzione con numero variabile di parametri.]]&lt;br /&gt;
&lt;br /&gt;
[[stampf - implementazione ridotta della printf.]]&lt;br /&gt;
&lt;br /&gt;
[[Producer&amp;amp;Consumer MP.]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2014.01.23]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.09.13]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.07.18]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.06.21]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.05.29]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.02.15]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.01.25]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeoria_2011.07.25]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2014.01.22]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.02.09]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.05.24]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.06.15]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.09.18]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2013.02.15]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2011.02.11]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica 2013.07.19]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica 2013.01.24]]&lt;br /&gt;
----&lt;br /&gt;
Ricordate che per creare un account o quando viene richiesto di risolvere un semplice calcolo occorre ricordare quanto scritto [[qui]]&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2013.09.13&amp;diff=486</id>
		<title>ProvaPratica 2013.09.13</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2013.09.13&amp;diff=486"/>
		<updated>2014-03-23T13:34:42Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[C esercizio 1 e 2]&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 /***********************************************************************&lt;br /&gt;
 * Prova Pratica di Laboratorio di Sistemi Operativi			*&lt;br /&gt;
 * 13 settembre 2013							*&lt;br /&gt;
 * Esercizio 1+2							*&lt;br /&gt;
 * 									*&lt;br /&gt;
 * URL: http://www.cs.unibo.it/~renzo/so/pratiche/2013.09.13.pdf 	*&lt;br /&gt;
 * Autore: Eduardo Santarelli						*&lt;br /&gt;
 ************************************************************************/&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/* An entry in the hash table. */&lt;br /&gt;
typedef struct fileEntry{&lt;br /&gt;
&lt;br /&gt;
	const char *filename;&lt;br /&gt;
	struct fileEntry *next;&lt;br /&gt;
&lt;br /&gt;
} fileEntry;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Global variables */&lt;br /&gt;
fileEntry **table = NULL;&lt;br /&gt;
int table_size;&lt;br /&gt;
&lt;br /&gt;
/* Hash function. Calculates XOR of 8-bytes blocks		*&lt;br /&gt;
 * (actually, sizeof(long int)) read from the input file	*/&lt;br /&gt;
unsigned int hash(const char *fname){&lt;br /&gt;
&lt;br /&gt;
	FILE *f;&lt;br /&gt;
	unsigned long int hash_val = 0;&lt;br /&gt;
	unsigned long int tmp = 0;&lt;br /&gt;
&lt;br /&gt;
	f = fopen(fname, &amp;quot;r&amp;quot;);&lt;br /&gt;
	fread(&amp;amp;hash_val, sizeof(unsigned long int), 1, f);&lt;br /&gt;
&lt;br /&gt;
	while((fread(&amp;amp;tmp, sizeof(unsigned long int), 1, f) == 1))&lt;br /&gt;
		hash_val = hash_val^tmp;&lt;br /&gt;
&lt;br /&gt;
	fclose(f);&lt;br /&gt;
	return hash_val;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Creates a table of struct fileEntry elements, based on the hash values of the files	*&lt;br /&gt;
 * detected by scandir. If a collision is detected, the new entry is pointed by the	*&lt;br /&gt;
 * 'next' field of the current last element*/&lt;br /&gt;
void populate_table(struct dirent *dir_list[], int n){&lt;br /&gt;
&lt;br /&gt;
	int i;&lt;br /&gt;
	&lt;br /&gt;
	table_size = 2*n;	&lt;br /&gt;
	table = (fileEntry**)calloc(table_size, sizeof(fileEntry));&lt;br /&gt;
	if(!table) abort();&lt;br /&gt;
&lt;br /&gt;
	for(i=0; i&amp;lt;n; i++){&lt;br /&gt;
		unsigned int index;&lt;br /&gt;
		fileEntry *entry = NULL;&lt;br /&gt;
&lt;br /&gt;
		entry = (fileEntry*)malloc(sizeof(fileEntry));&lt;br /&gt;
		if(!entry) abort();&lt;br /&gt;
&lt;br /&gt;
		index = hash(dir_list[i]-&amp;gt;d_name)%table_size;&lt;br /&gt;
		if(table[index] == NULL){&lt;br /&gt;
			entry-&amp;gt;filename = dir_list[i]-&amp;gt;d_name;&lt;br /&gt;
			entry-&amp;gt;next = NULL;&lt;br /&gt;
			table[index] = entry;&lt;br /&gt;
		}&lt;br /&gt;
		else{&lt;br /&gt;
			entry-&amp;gt;filename = dir_list[i]-&amp;gt;d_name;&lt;br /&gt;
			entry-&amp;gt;next = table[index];&lt;br /&gt;
			table[index] = entry;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/* Compares two files */&lt;br /&gt;
int fcompare(const char *f1, const char *f2){&lt;br /&gt;
&lt;br /&gt;
	FILE *f, *g;&lt;br /&gt;
	int file1, file2;	&lt;br /&gt;
&lt;br /&gt;
	f = fopen(f1, &amp;quot;r&amp;quot;);&lt;br /&gt;
	g = fopen(f2, &amp;quot;r&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
	while(fread(&amp;amp;file1, sizeof(int), 1, f) &amp;amp;&amp;amp; fread(&amp;amp;file2, sizeof(int), 1, g)){&lt;br /&gt;
		if(file1!=file2){&lt;br /&gt;
			fclose(f);&lt;br /&gt;
			fclose(g);&lt;br /&gt;
			return 0;&lt;br /&gt;
 		}&lt;br /&gt;
	}&lt;br /&gt;
	fclose(f);&lt;br /&gt;
	fclose(g);&lt;br /&gt;
	return 1;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
		&lt;br /&gt;
&lt;br /&gt;
/* This function scans the file table. If an entry contains more than one file,	*&lt;br /&gt;
 * it compares all them. If a duplicate is found, the first file is removed,  	*&lt;br /&gt;
 * and a hard link is created with the deleted file's name, pointing to the	*&lt;br /&gt;
 * second, identical, file.							*/&lt;br /&gt;
void deduplicate(){&lt;br /&gt;
&lt;br /&gt;
	int i;&lt;br /&gt;
&lt;br /&gt;
	for(i=0; i&amp;lt;table_size; i++){&lt;br /&gt;
		while(table[i] != NULL &amp;amp;&amp;amp; table[i]-&amp;gt;next != NULL){&lt;br /&gt;
&lt;br /&gt;
			fileEntry *file1, *file2;&lt;br /&gt;
			file1 = table[i];&lt;br /&gt;
			file2 = table[i]-&amp;gt;next;&lt;br /&gt;
			while(file2 != NULL){&lt;br /&gt;
				if(fcompare(file1-&amp;gt;filename, file2-&amp;gt;filename)){&lt;br /&gt;
					unlink(file1-&amp;gt;filename);&lt;br /&gt;
					link(file2-&amp;gt;filename, file1-&amp;gt;filename);&lt;br /&gt;
					printf(&amp;quot;%s -&amp;gt; %s\n&amp;quot;, file1-&amp;gt;filename, file2-&amp;gt;filename);&lt;br /&gt;
					break;&lt;br /&gt;
				}&lt;br /&gt;
				file2 = file2-&amp;gt;next;&lt;br /&gt;
			}&lt;br /&gt;
			table[i] = table[i]-&amp;gt;next;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*selector function for scandir() */&lt;br /&gt;
int exclude(const struct dirent *dir){&lt;br /&gt;
&lt;br /&gt;
	if(dir-&amp;gt;d_type != DT_REG)&lt;br /&gt;
		return 0;&lt;br /&gt;
	else&lt;br /&gt;
		return 1;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]){&lt;br /&gt;
&lt;br /&gt;
	struct dirent **entries = NULL;&lt;br /&gt;
	int num_files;&lt;br /&gt;
&lt;br /&gt;
	num_files = scandir(&amp;quot;.&amp;quot;, &amp;amp;entries, exclude, alphasort);&lt;br /&gt;
&lt;br /&gt;
	populate_table(entries, num_files);&lt;br /&gt;
	deduplicate();&lt;br /&gt;
&lt;br /&gt;
	return EXIT_SUCCESS;&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
------&lt;br /&gt;
&lt;br /&gt;
[Python esercizio 3]&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
 &lt;br /&gt;
# Prova Pratica di Laboratorio di Sistemi Operativi&lt;br /&gt;
# 13 settembre 2013&lt;br /&gt;
# Esercizio 3&lt;br /&gt;
#&lt;br /&gt;
# URL: http://www.cs.unibo.it/~renzo/so/pratiche/2013.09.13.pdf 	*&lt;br /&gt;
# Autore: Eduardo Santarelli&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class LineCounter:&lt;br /&gt;
&lt;br /&gt;
    def __init__(self, fileList):&lt;br /&gt;
        # A list containing the char-per-line&lt;br /&gt;
        # count for the specified files.&lt;br /&gt;
        # Value for line 1 is in result[0] and so on.&lt;br /&gt;
        self.result = []&lt;br /&gt;
        self.files = fileList # List of files to elaborate.&lt;br /&gt;
        self.getLinesCount()&lt;br /&gt;
&lt;br /&gt;
    # Count the chars for each lines of specified file,&lt;br /&gt;
    # add the result to the relevant field in self.result,&lt;br /&gt;
    # if already present, append the value to the&lt;br /&gt;
    # end of the list if not.&lt;br /&gt;
    def countLines(self, file):&lt;br /&gt;
        i = 0&lt;br /&gt;
        fd = open(file)&lt;br /&gt;
        for line in fd:&lt;br /&gt;
            lineLength = len(line)&lt;br /&gt;
            # If an entry for the current line does&lt;br /&gt;
            # not exist, append the value to the end of the list&lt;br /&gt;
            try:&lt;br /&gt;
                self.result[i] += lineLength&lt;br /&gt;
            except IndexError:&lt;br /&gt;
                self.result.append(lineLength)&lt;br /&gt;
            i = i+1&lt;br /&gt;
        fd.close()&lt;br /&gt;
&lt;br /&gt;
    # Call self.countLines() for each file in self.files&lt;br /&gt;
    # in order to build the self.result list&lt;br /&gt;
    def getLinesCount(self):&lt;br /&gt;
        for entry in self.files:&lt;br /&gt;
            self.countLines(entry)&lt;br /&gt;
&lt;br /&gt;
    # Print the items in self.result, each preceded by the&lt;br /&gt;
    # corresponding line number.&lt;br /&gt;
    def printLines(self):&lt;br /&gt;
        i = 1&lt;br /&gt;
        for value in self.result:&lt;br /&gt;
            print(&amp;quot;{:d}: {:d}&amp;quot;.format(i, value))&lt;br /&gt;
            i = i+1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
import os&lt;br /&gt;
&lt;br /&gt;
lc = LineCounter(os.listdir(os.getcwd()))&lt;br /&gt;
lc.printLines()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[Bash esercizio 3]&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Bash&amp;quot;&amp;gt;&lt;br /&gt;
#Esercizio 3: Script bash o Python: (10 punti):&lt;br /&gt;
#Sia data una directory che contiene file di testo.&lt;br /&gt;
#Scopo dell'esercizio e' di contare i caratteri delle corrispondenti righe di testo di tutti i file della directory, si vuole cioe' sapere&lt;br /&gt;
#il numero totale di caratteri presenti nelle prime righe di tutti i file, nelle seconde linee, ecc.&lt;br /&gt;
#$ ccpl mydir&lt;br /&gt;
#1 234&lt;br /&gt;
#2 21&lt;br /&gt;
#3 333&lt;br /&gt;
# .....&lt;br /&gt;
#l'ouput significa che se contiamo tutti i caratteri contenuti nella prima riga di tutti i file in mydir otteniamo 234 (mydir/file1&lt;br /&gt;
#puo' avere 40 caratteri nella prima riga, mydir/file2 ne puo' avere 20, ecc... procedendo per tutti i file di mydir la somma fa 234)&lt;br /&gt;
&lt;br /&gt;
#! bin/bash&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
nline=1&lt;br /&gt;
touch fileresult&lt;br /&gt;
paste $1/* &amp;gt; fileresult             #merges every file's line from the directory passed to the file fileresult&lt;br /&gt;
while read line; do&lt;br /&gt;
	count=$(echo $line | wc -m)      #counts the number of char in each line of fileresult&lt;br /&gt;
	echo  $nline $count&lt;br /&gt;
	nline=$(($nline + 1))	&lt;br /&gt;
done &amp;lt; fileresult&lt;br /&gt;
rm fileresult&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Pirata&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Producer%26Consumer_MP.&amp;diff=484</id>
		<title>Producer&amp;Consumer MP.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Producer%26Consumer_MP.&amp;diff=484"/>
		<updated>2014-03-21T19:31:39Z</updated>

		<summary type="html">&lt;p&gt;Pirata: Created page with &amp;quot;&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt; MP-sincrono dato quello asincrono  Producer(){ 	produce(x); 	asend(x,Consumer); 	while(1){ 		areceive(Consumer); 		produce(x); 		asend(x,Consumer); ...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
MP-sincrono dato quello asincrono&lt;br /&gt;
&lt;br /&gt;
Producer(){&lt;br /&gt;
	produce(x);&lt;br /&gt;
	asend(x,Consumer);&lt;br /&gt;
	while(1){&lt;br /&gt;
		areceive(Consumer);&lt;br /&gt;
		produce(x);&lt;br /&gt;
		asend(x,Consumer);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consumer(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		x = areceive(Producer);&lt;br /&gt;
		consume(x);&lt;br /&gt;
		asend(ACK,Producer);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
MP-asincrono dato quello sincrono&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;slideconcorrenzapg201.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Producer(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		produce(x);&lt;br /&gt;
		asend(x,Consumer);	&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
Consumer(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		x=areceive(Producer);&lt;br /&gt;
		consume(x);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Fede&amp;amp;Pirata&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Main_Page&amp;diff=483</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Main_Page&amp;diff=483"/>
		<updated>2014-03-21T19:27:04Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Questo &amp;amp;egrave; il Wiki del Corso di Sistemi Operativi&lt;br /&gt;
&lt;br /&gt;
[[Esercizi a caso del Prof.]]&lt;br /&gt;
&lt;br /&gt;
[[Decalogo di Programmazione Concorrente]]&lt;br /&gt;
&lt;br /&gt;
[[Python Programma tieni punteggio.]]&lt;br /&gt;
&lt;br /&gt;
[[Comandi visti alle lezioni.]]&lt;br /&gt;
&lt;br /&gt;
[[SYS CALL viste a lezione.]]&lt;br /&gt;
&lt;br /&gt;
[[Parametri con getopt().]]&lt;br /&gt;
&lt;br /&gt;
[[Funzione con numero variabile di parametri.]]&lt;br /&gt;
&lt;br /&gt;
[[stampf - implementazione ridotta della printf.]]&lt;br /&gt;
&lt;br /&gt;
[[Producer&amp;amp;Consumer MP.]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.09.13]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.07.18]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.06.21]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.05.29]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.02.15]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.01.25]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeoria_2011.07.25]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2014.01.22]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.02.09]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.05.24]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.06.15]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.09.18]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2013.02.15]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2011.02.11]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica 2013.07.19]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica 2013.01.24]]&lt;br /&gt;
----&lt;br /&gt;
Ricordate che per creare un account o quando viene richiesto di risolvere un semplice calcolo occorre ricordare quanto scritto [[qui]]&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Main_Page&amp;diff=482</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Main_Page&amp;diff=482"/>
		<updated>2014-03-21T19:26:00Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Questo &amp;amp;egrave; il Wiki del Corso di Sistemi Operativi&lt;br /&gt;
&lt;br /&gt;
[[Esercizi a caso del Prof.]]&lt;br /&gt;
&lt;br /&gt;
[[Decalogo di Programmazione Concorrente]]&lt;br /&gt;
&lt;br /&gt;
[[Python Programma tieni punteggio.]]&lt;br /&gt;
&lt;br /&gt;
[[Comandi visti alle lezioni.]]&lt;br /&gt;
&lt;br /&gt;
[[SYS CALL viste a lezione.]]&lt;br /&gt;
&lt;br /&gt;
[[Parametri con getopt().]]&lt;br /&gt;
&lt;br /&gt;
[[Funzione con numero variabile di parametri.]]&lt;br /&gt;
&lt;br /&gt;
[[stampf - implementazione ridotta della printf.]]&lt;br /&gt;
&lt;br /&gt;
[[Producer&amp;amp;Consumer MP]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.09.13]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.07.18]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.06.21]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.05.29]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.02.15]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.01.25]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeoria_2011.07.25]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2014.01.22]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.02.09]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.05.24]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.06.15]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.09.18]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2013.02.15]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2011.02.11]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica 2013.07.19]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica 2013.01.24]]&lt;br /&gt;
----&lt;br /&gt;
Ricordate che per creare un account o quando viene richiesto di risolvere un semplice calcolo occorre ricordare quanto scritto [[qui]]&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Producer%26Consumer_MP-asincrono&amp;diff=481</id>
		<title>Producer&amp;Consumer MP-asincrono</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Producer%26Consumer_MP-asincrono&amp;diff=481"/>
		<updated>2014-03-21T19:25:38Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
MP-sincrono dato quello asincrono &lt;br /&gt;
&lt;br /&gt;
Producer(){&lt;br /&gt;
	produce(x);&lt;br /&gt;
	asend(x,Consumer);&lt;br /&gt;
	while(1){&lt;br /&gt;
		areceive(Consumer);&lt;br /&gt;
		produce(x);&lt;br /&gt;
		asend(x,Consumer);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consumer(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		x = areceive(Producer);&lt;br /&gt;
		consume(x);&lt;br /&gt;
		asend(ACK,Producer);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
MP-asincrono dato quello sincrono&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;slideconcorrenzapg201.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Producer(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		produce(x);&lt;br /&gt;
		asend(x,Consumer);	&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
Consumer(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		x=areceive(Producer);&lt;br /&gt;
		consume(x);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Fede &amp;amp; Pirata&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Producer%26Consumer_MP-asincrono&amp;diff=480</id>
		<title>Producer&amp;Consumer MP-asincrono</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Producer%26Consumer_MP-asincrono&amp;diff=480"/>
		<updated>2014-03-21T17:08:31Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
MP-sincrono dato quello asincrono &lt;br /&gt;
&lt;br /&gt;
Procuder(){&lt;br /&gt;
	produce(x);&lt;br /&gt;
	asend(x,Consumer);&lt;br /&gt;
	while(1){&lt;br /&gt;
		areceive(Consumer);&lt;br /&gt;
		produce(x);&lt;br /&gt;
		asend(x,Consumer);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consumer(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		x = areceive(Producer);&lt;br /&gt;
		consume(x);&lt;br /&gt;
		asend(ACK,Producer);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
MP-asincrono dato quello sincrono&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;slideconcorrenzapg201.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Procuder(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		produce(x);&lt;br /&gt;
		asend(x,Consumer);	&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
Consumer(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		x=areceive(Producer);&lt;br /&gt;
		consume(x);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Fede &amp;amp; Pirata&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Producer%26Consumer_MP-asincrono&amp;diff=479</id>
		<title>Producer&amp;Consumer MP-asincrono</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Producer%26Consumer_MP-asincrono&amp;diff=479"/>
		<updated>2014-03-21T17:05:03Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
MP-asincrono &lt;br /&gt;
&lt;br /&gt;
Procuder(){&lt;br /&gt;
	produce(x);&lt;br /&gt;
	asend(x,Consumer);&lt;br /&gt;
	while(1){&lt;br /&gt;
		areceive(Consumer);&lt;br /&gt;
		produce(x);&lt;br /&gt;
		asend(x,Consumer);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consumer(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		x = areceive(Producer);&lt;br /&gt;
		consume(x);&lt;br /&gt;
		asend(ACK,Producer);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
MP-asincrono dato quello sincrono&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;slideconcorrenzapg201.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Procuder(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		produce(x);&lt;br /&gt;
		asend(x,Consumer);	&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
Consumer(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		x=areceive(Producer);&lt;br /&gt;
		consume(x);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Fede &amp;amp; Pirata&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Producer%26Consumer_MP-asincrono&amp;diff=478</id>
		<title>Producer&amp;Consumer MP-asincrono</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Producer%26Consumer_MP-asincrono&amp;diff=478"/>
		<updated>2014-03-21T17:04:23Z</updated>

		<summary type="html">&lt;p&gt;Pirata: Created page with &amp;quot;&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt; MP-asincrono   Procuder(){ 	produce(x); 	asend(x,Consumer); 	while(1){ 		areceive(Consumer); 		produce(x); 		asend(x,Consumer); 	} }   Consumer(){ 	...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
MP-asincrono &lt;br /&gt;
&lt;br /&gt;
Procuder(){&lt;br /&gt;
	produce(x);&lt;br /&gt;
	asend(x,Consumer);&lt;br /&gt;
	while(1){&lt;br /&gt;
		areceive(Consumer);&lt;br /&gt;
		produce(x);&lt;br /&gt;
		asend(x,Consumer);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consumer(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		x = areceive(Producer);&lt;br /&gt;
		consume(x);&lt;br /&gt;
		asend(ACK,Producer);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
MP-asincrono dato quello sincrono&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;slideconcorrenzapg201.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Procuder(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		produce(x);&lt;br /&gt;
		asend(x,Consumer);	&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
Consumer(){&lt;br /&gt;
	while(1){&lt;br /&gt;
		x=areceive(Producer);&lt;br /&gt;
		consume(x);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Main_Page&amp;diff=477</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Main_Page&amp;diff=477"/>
		<updated>2014-03-21T17:02:44Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Questo &amp;amp;egrave; il Wiki del Corso di Sistemi Operativi&lt;br /&gt;
&lt;br /&gt;
[[Esercizi a caso del Prof.]]&lt;br /&gt;
&lt;br /&gt;
[[Decalogo di Programmazione Concorrente]]&lt;br /&gt;
&lt;br /&gt;
[[Python Programma tieni punteggio.]]&lt;br /&gt;
&lt;br /&gt;
[[Comandi visti alle lezioni.]]&lt;br /&gt;
&lt;br /&gt;
[[SYS CALL viste a lezione.]]&lt;br /&gt;
&lt;br /&gt;
[[Parametri con getopt().]]&lt;br /&gt;
&lt;br /&gt;
[[Funzione con numero variabile di parametri.]]&lt;br /&gt;
&lt;br /&gt;
[[stampf - implementazione ridotta della printf.]]&lt;br /&gt;
&lt;br /&gt;
[[Producer&amp;amp;Consumer MP-asincrono]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.09.13]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.07.18]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.06.21]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.05.29]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.02.15]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaPratica 2013.01.25]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeoria_2011.07.25]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2014.01.22]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.02.09]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.05.24]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.06.15]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2012.09.18]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2013.02.15]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica_2011.02.11]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica 2013.07.19]]&lt;br /&gt;
&lt;br /&gt;
[[ProvaTeorica 2013.01.24]]&lt;br /&gt;
----&lt;br /&gt;
Ricordate che per creare un account o quando viene richiesto di risolvere un semplice calcolo occorre ricordare quanto scritto [[qui]]&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=384</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=384"/>
		<updated>2013-12-04T10:48:16Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* SIGNAL */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===LSEEK/READ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Read: legge da un file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1(sparse file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio3(copia di uno sparse file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparse file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio4(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
Esempio(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===DUP===&lt;br /&gt;
&lt;br /&gt;
*Dup: duplica il file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[]){&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	close(1);&lt;br /&gt;
	dup(fd);   //dup2(fd,1) produce lo stesso effetto&lt;br /&gt;
	printf(&amp;quot;Hello World \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===FUNZIONI SULLE DIRECTORY===&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int mystrcmp( const void *a , const void *b ){&lt;br /&gt;
	char **pa = (char **)a;&lt;br /&gt;
	char **pb = (char **)b;&lt;br /&gt;
	return strcmp( *pa , *pb );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	DIR *d;&lt;br /&gt;
	struct dirent *de;&lt;br /&gt;
	char **names;&lt;br /&gt;
	int i , count = 0;&lt;br /&gt;
	d = opendir( argv[1] );     &lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		count++;&lt;br /&gt;
	rewinddir(d);&lt;br /&gt;
	names = (char **)calloc( count , sizeof(char *) );&lt;br /&gt;
	count=0;&lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		names[count++] = strdup( de-&amp;gt;d_name );&lt;br /&gt;
	closedir(d);&lt;br /&gt;
	qsort( names , count , sizeof(char *) , mystrcmp );&lt;br /&gt;
	for( i = 0 ; i &amp;lt; count ; i++ )&lt;br /&gt;
		printf(&amp;quot; %s &amp;quot;, names[i] );&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include&amp;lt;string.h&amp;gt;&lt;br /&gt;
#include&amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int count;&lt;br /&gt;
	int i;&lt;br /&gt;
	struct dirent **de;&lt;br /&gt;
	count = scandir( argv[1] , &amp;amp;de , NULL , alphasort );&lt;br /&gt;
	for ( i = 0 ; i &amp;lt; count ; i++ ){&lt;br /&gt;
		printf(&amp;quot;%s &amp;quot;,de[i]-&amp;gt;d_name);&lt;br /&gt;
		free(de[i]);&lt;br /&gt;
	}&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	free(de);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Segnali==&lt;br /&gt;
===SIGNAL===&lt;br /&gt;
&lt;br /&gt;
Esempio1(programma riluttante a morire):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void no( int sn )&lt;br /&gt;
{&lt;br /&gt;
	printf(&amp;quot;io non termino! %d\n&amp;quot; , sn );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv )&lt;br /&gt;
{&lt;br /&gt;
	signal( SIGTERM , no );&lt;br /&gt;
	signal( SIGINT , no );&lt;br /&gt;
	while(1){&lt;br /&gt;
		pause();&lt;br /&gt;
		printf(&amp;quot;ciao\n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(il figlio termina):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
#include&amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include&amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void child( int sn )&lt;br /&gt;
{&lt;br /&gt;
	int status;&lt;br /&gt;
	wait(&amp;amp;status);&lt;br /&gt;
	printf(&amp;quot;il figlio termina! \n&amp;quot; );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv )&lt;br /&gt;
{&lt;br /&gt;
	signal( SIGCHLD , child );&lt;br /&gt;
	if ( fork() &amp;gt; 0 ){&lt;br /&gt;
	while(1)&lt;br /&gt;
		pause();&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		sleep(3);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		} &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=383</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=383"/>
		<updated>2013-12-03T19:01:13Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* SIGNAL */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===LSEEK/READ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Read: legge da un file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1(sparse file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio3(copia di uno sparse file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparse file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio4(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
Esempio(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===DUP===&lt;br /&gt;
&lt;br /&gt;
*Dup: duplica il file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[]){&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	close(1);&lt;br /&gt;
	dup(fd);   //dup2(fd,1) produce lo stesso effetto&lt;br /&gt;
	printf(&amp;quot;Hello World \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===FUNZIONI SULLE DIRECTORY===&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int mystrcmp( const void *a , const void *b ){&lt;br /&gt;
	char **pa = (char **)a;&lt;br /&gt;
	char **pb = (char **)b;&lt;br /&gt;
	return strcmp( *pa , *pb );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	DIR *d;&lt;br /&gt;
	struct dirent *de;&lt;br /&gt;
	char **names;&lt;br /&gt;
	int i , count = 0;&lt;br /&gt;
	d = opendir( argv[1] );     &lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		count++;&lt;br /&gt;
	rewinddir(d);&lt;br /&gt;
	names = (char **)calloc( count , sizeof(char *) );&lt;br /&gt;
	count=0;&lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		names[count++] = strdup( de-&amp;gt;d_name );&lt;br /&gt;
	closedir(d);&lt;br /&gt;
	qsort( names , count , sizeof(char *) , mystrcmp );&lt;br /&gt;
	for( i = 0 ; i &amp;lt; count ; i++ )&lt;br /&gt;
		printf(&amp;quot; %s &amp;quot;, names[i] );&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include&amp;lt;string.h&amp;gt;&lt;br /&gt;
#include&amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int count;&lt;br /&gt;
	int i;&lt;br /&gt;
	struct dirent **de;&lt;br /&gt;
	count = scandir( argv[1] , &amp;amp;de , NULL , alphasort );&lt;br /&gt;
	for ( i = 0 ; i &amp;lt; count ; i++ ){&lt;br /&gt;
		printf(&amp;quot;%s &amp;quot;,de[i]-&amp;gt;d_name);&lt;br /&gt;
		free(de[i]);&lt;br /&gt;
	}&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	free(de);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Segnali==&lt;br /&gt;
===SIGNAL===&lt;br /&gt;
&lt;br /&gt;
Esempio1(programma riluttante a morire):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void no( int sn )&lt;br /&gt;
{&lt;br /&gt;
	printf(&amp;quot;io non termino! %d\n&amp;quot; , sn );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main( int arg , char *argv )&lt;br /&gt;
{&lt;br /&gt;
	signal( SIGTERM , no );&lt;br /&gt;
	signal( SIGINT , no );&lt;br /&gt;
	while(1){&lt;br /&gt;
		pause();&lt;br /&gt;
		printf(&amp;quot;ciao\n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(il figlio termina):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
#include&amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include&amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void child( int sn )&lt;br /&gt;
{&lt;br /&gt;
	int status;&lt;br /&gt;
	wait(&amp;amp;status);&lt;br /&gt;
	printf(&amp;quot;il figlio termina! \n&amp;quot; );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main( int arg , char *argv )&lt;br /&gt;
{&lt;br /&gt;
	signal( SIGCHLD , child );&lt;br /&gt;
	if ( fork() &amp;gt; 0 ){&lt;br /&gt;
	while(1)&lt;br /&gt;
		pause();&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		sleep(3);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		} &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=382</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=382"/>
		<updated>2013-12-03T18:41:44Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* SEGNALI */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===LSEEK/READ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Read: legge da un file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1(sparse file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio3(copia di uno sparse file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparse file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio4(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
Esempio(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===DUP===&lt;br /&gt;
&lt;br /&gt;
*Dup: duplica il file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[]){&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	close(1);&lt;br /&gt;
	dup(fd);   //dup2(fd,1) produce lo stesso effetto&lt;br /&gt;
	printf(&amp;quot;Hello World \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===FUNZIONI SULLE DIRECTORY===&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int mystrcmp( const void *a , const void *b ){&lt;br /&gt;
	char **pa = (char **)a;&lt;br /&gt;
	char **pb = (char **)b;&lt;br /&gt;
	return strcmp( *pa , *pb );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	DIR *d;&lt;br /&gt;
	struct dirent *de;&lt;br /&gt;
	char **names;&lt;br /&gt;
	int i , count = 0;&lt;br /&gt;
	d = opendir( argv[1] );     &lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		count++;&lt;br /&gt;
	rewinddir(d);&lt;br /&gt;
	names = (char **)calloc( count , sizeof(char *) );&lt;br /&gt;
	count=0;&lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		names[count++] = strdup( de-&amp;gt;d_name );&lt;br /&gt;
	closedir(d);&lt;br /&gt;
	qsort( names , count , sizeof(char *) , mystrcmp );&lt;br /&gt;
	for( i = 0 ; i &amp;lt; count ; i++ )&lt;br /&gt;
		printf(&amp;quot; %s &amp;quot;, names[i] );&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include&amp;lt;string.h&amp;gt;&lt;br /&gt;
#include&amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int count;&lt;br /&gt;
	int i;&lt;br /&gt;
	struct dirent **de;&lt;br /&gt;
	count = scandir( argv[1] , &amp;amp;de , NULL , alphasort );&lt;br /&gt;
	for ( i = 0 ; i &amp;lt; count ; i++ ){&lt;br /&gt;
		printf(&amp;quot;%s &amp;quot;,de[i]-&amp;gt;d_name);&lt;br /&gt;
		free(de[i]);&lt;br /&gt;
	}&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	free(de);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Segnali==&lt;br /&gt;
===SIGNAL===&lt;br /&gt;
&lt;br /&gt;
Esempio1(programma riluttante a morire):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void no( int sn )&lt;br /&gt;
{&lt;br /&gt;
	printf(&amp;quot;io non termino! %d\n&amp;quot; , sn );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main( int arg , char *argv )&lt;br /&gt;
{&lt;br /&gt;
	signal( SIGTERM , no );&lt;br /&gt;
	signal( SIGINT , no );&lt;br /&gt;
	while(1){&lt;br /&gt;
		pause();&lt;br /&gt;
		printf(&amp;quot;ciao\n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=381</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=381"/>
		<updated>2013-12-03T18:41:24Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===LSEEK/READ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Read: legge da un file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1(sparse file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio3(copia di uno sparse file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparse file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio4(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
Esempio(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===DUP===&lt;br /&gt;
&lt;br /&gt;
*Dup: duplica il file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[]){&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	close(1);&lt;br /&gt;
	dup(fd);   //dup2(fd,1) produce lo stesso effetto&lt;br /&gt;
	printf(&amp;quot;Hello World \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===FUNZIONI SULLE DIRECTORY===&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int mystrcmp( const void *a , const void *b ){&lt;br /&gt;
	char **pa = (char **)a;&lt;br /&gt;
	char **pb = (char **)b;&lt;br /&gt;
	return strcmp( *pa , *pb );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	DIR *d;&lt;br /&gt;
	struct dirent *de;&lt;br /&gt;
	char **names;&lt;br /&gt;
	int i , count = 0;&lt;br /&gt;
	d = opendir( argv[1] );     &lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		count++;&lt;br /&gt;
	rewinddir(d);&lt;br /&gt;
	names = (char **)calloc( count , sizeof(char *) );&lt;br /&gt;
	count=0;&lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		names[count++] = strdup( de-&amp;gt;d_name );&lt;br /&gt;
	closedir(d);&lt;br /&gt;
	qsort( names , count , sizeof(char *) , mystrcmp );&lt;br /&gt;
	for( i = 0 ; i &amp;lt; count ; i++ )&lt;br /&gt;
		printf(&amp;quot; %s &amp;quot;, names[i] );&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include&amp;lt;string.h&amp;gt;&lt;br /&gt;
#include&amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int count;&lt;br /&gt;
	int i;&lt;br /&gt;
	struct dirent **de;&lt;br /&gt;
	count = scandir( argv[1] , &amp;amp;de , NULL , alphasort );&lt;br /&gt;
	for ( i = 0 ; i &amp;lt; count ; i++ ){&lt;br /&gt;
		printf(&amp;quot;%s &amp;quot;,de[i]-&amp;gt;d_name);&lt;br /&gt;
		free(de[i]);&lt;br /&gt;
	}&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	free(de);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SEGNALI==&lt;br /&gt;
===SIGNAL===&lt;br /&gt;
&lt;br /&gt;
Esempio1(programma riluttante a morire):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;signal.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void no( int sn )&lt;br /&gt;
{&lt;br /&gt;
	printf(&amp;quot;io non termino! %d\n&amp;quot; , sn );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main( int arg , char *argv )&lt;br /&gt;
{&lt;br /&gt;
	signal( SIGTERM , no );&lt;br /&gt;
	signal( SIGINT , no );&lt;br /&gt;
	while(1){&lt;br /&gt;
		pause();&lt;br /&gt;
		printf(&amp;quot;ciao\n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2013.05.29&amp;diff=380</id>
		<title>ProvaPratica 2013.05.29</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2013.05.29&amp;diff=380"/>
		<updated>2013-12-03T18:16:57Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[Python 3]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
Prova Pratica di Laboratorio di Sistemi Operativi&lt;br /&gt;
29 maggio 2013&lt;br /&gt;
Esercizio 3&lt;br /&gt;
&lt;br /&gt;
URL: http://www.cs.unibo.it/~renzo/so/pratiche/2013.05.29.pdf&lt;br /&gt;
&lt;br /&gt;
@author: Tommaso Ognibene&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
import os, sys&lt;br /&gt;
&lt;br /&gt;
def Main(argv):&lt;br /&gt;
    # Check number of arguments&lt;br /&gt;
    if len(argv) != 2:&lt;br /&gt;
        print(&amp;quot;The function requires one argument to be passed in.&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
    &lt;br /&gt;
    # Check parameters&lt;br /&gt;
    topDir = str(sys.argv[1])&lt;br /&gt;
    if not os.path.isdir(topDir):&lt;br /&gt;
        print(&amp;quot;The parameter should be an existing directory.&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
    &lt;br /&gt;
    # Build a dictionary with key-value pair {file extension - total size}&lt;br /&gt;
    extensionSize = { }&lt;br /&gt;
    GetSize(topDir, extensionSize)&lt;br /&gt;
    &lt;br /&gt;
    # Print results&lt;br /&gt;
    PrintResults(extensionSize)&lt;br /&gt;
&lt;br /&gt;
def GetSize(topDir, extensionSize):&lt;br /&gt;
    for dirPath, dirNames, files in os.walk(topDir):&lt;br /&gt;
        for file in files:&lt;br /&gt;
            # 'example.mp3' -&amp;gt; ['example', 'mp3']&lt;br /&gt;
            # 'example.tar.gz' -&amp;gt; ['example', 'tar', 'gz']&lt;br /&gt;
            parts = file.split('.')&lt;br /&gt;
            # ['example', 'mp3'] -&amp;gt; ['mp3']&lt;br /&gt;
            # ['example', 'tar', 'gz'] -&amp;gt; ['tar', 'gz']&lt;br /&gt;
            parts = parts[1:]&lt;br /&gt;
            # ['mp3'] -&amp;gt; '.mp3'&lt;br /&gt;
            # ['tar', 'gz'] -&amp;gt; '.tar.gz'&lt;br /&gt;
            fileExtension = &amp;quot;.{0}&amp;quot;.format(&amp;quot;.&amp;quot;.join(str(part) for part in parts))&lt;br /&gt;
            &lt;br /&gt;
            # Compute the size in Bytes and update the dictionary&lt;br /&gt;
            filePath = os.path.join(dirPath, file)   &lt;br /&gt;
            fileSize = os.path.getsize(filePath)&lt;br /&gt;
            extensionSize[fileExtension] = extensionSize.get(fileExtension, 0) + fileSize&lt;br /&gt;
&lt;br /&gt;
# Print results&lt;br /&gt;
def PrintResults(extensionSize):&lt;br /&gt;
    for key, value in sorted(extensionSize.items()):&lt;br /&gt;
        print('{0}: {1} Bytes.'.format(key, value))&lt;br /&gt;
        &lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    sys.exit(Main(sys.argv))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
mia versione&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
import os, sys, copy&lt;br /&gt;
&lt;br /&gt;
def dotsubstr(a):#restituisce la sottostringa .suffisso&lt;br /&gt;
	#fcd = os.listdir('{0}'.format(arg1))&lt;br /&gt;
	i=0&lt;br /&gt;
	try:&lt;br /&gt;
		while a[i]!='.':&lt;br /&gt;
			i=i+1&lt;br /&gt;
		return a[i:]&lt;br /&gt;
	except IndexError:&lt;br /&gt;
		return -1&lt;br /&gt;
&lt;br /&gt;
def compliarg(li,arg):#restituisce una lista di tutti gli elementi contenenti la sottostringa arg come suffisso&lt;br /&gt;
	res=[]&lt;br /&gt;
	while li != []:&lt;br /&gt;
		a=li.pop()&lt;br /&gt;
		if a.endswith(arg): res.append(a)&lt;br /&gt;
	return res&lt;br /&gt;
&lt;br /&gt;
def listremintsect(l2,l1):#restituisce una lista res = l1 - intersezione di l1 ed l2&lt;br /&gt;
	res=[]&lt;br /&gt;
	while l1 != []:&lt;br /&gt;
		a=l1.pop()&lt;br /&gt;
		if not a in l2: res.append(a)&lt;br /&gt;
	return res&lt;br /&gt;
&lt;br /&gt;
def createpathlist(c,path):#restituisce una lista di path 'path' relativi ai file contenuti in c.&lt;br /&gt;
	res = []&lt;br /&gt;
	while c != []:&lt;br /&gt;
		res.append('{0}/{1}'.format(path,c.pop()))&lt;br /&gt;
	return res&lt;br /&gt;
&lt;br /&gt;
def totsizes(d): #data una lista d di path restituisce la somma di tutti i size di d&lt;br /&gt;
	res = 0&lt;br /&gt;
	while d != []:&lt;br /&gt;
		a = d.pop()&lt;br /&gt;
		if os.path.isfile('{0}'.format(a)):&lt;br /&gt;
			res = res + os.path.getsize('{0}'.format(a))&lt;br /&gt;
	return res&lt;br /&gt;
&lt;br /&gt;
def listsubstr(arg): #ritorna un dizionario del tipo diz[str(suffisso)] = int(size relativo al suffisso)&lt;br /&gt;
	res = {}&lt;br /&gt;
	fcd = os.listdir('{0}'.format(arg))&lt;br /&gt;
	while fcd != []:&lt;br /&gt;
		fcdtmp=copy.deepcopy(fcd) #BUGGONE SENZA COPY!!!!!!&lt;br /&gt;
		a = fcd.pop()&lt;br /&gt;
		b = dotsubstr(a)&lt;br /&gt;
		if b == -1: continue&lt;br /&gt;
		else: pass&lt;br /&gt;
		c = compliarg(fcdtmp,b)&lt;br /&gt;
		s=copy.deepcopy(c) #!!!!!!!!!!!!!!!!!!!!!!!!!&lt;br /&gt;
		d = createpathlist(c,arg)&lt;br /&gt;
		res[b] = totsizes(d)&lt;br /&gt;
&lt;br /&gt;
		fcd = listremintsect(s,fcd)&lt;br /&gt;
&lt;br /&gt;
	return res&lt;br /&gt;
		&lt;br /&gt;
&lt;br /&gt;
try:&lt;br /&gt;
	res = listsubstr(sys.argv[1])&lt;br /&gt;
	a=list(res.keys())&lt;br /&gt;
	while a != []:&lt;br /&gt;
		b = a.pop()&lt;br /&gt;
		print('{0}:\t{1}'.format(b,res[b]))&lt;br /&gt;
except OSError:&lt;br /&gt;
	print(&amp;quot;Could not solve path&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-fede&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Esercizio 1: Linguaggio C&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/eventfd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 10&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]){&lt;br /&gt;
	int efd,n,i;&lt;br /&gt;
	char buf1[BUFFSIZE];&lt;br /&gt;
	char buf2[BUFFSIZE];&lt;br /&gt;
	&lt;br /&gt;
	do{&lt;br /&gt;
		efd = eventfd(0,0);&lt;br /&gt;
	}while(efd&amp;lt;0);&lt;br /&gt;
	&lt;br /&gt;
	while(1){&lt;br /&gt;
		if(fork()){ /*padre*/&lt;br /&gt;
				read(0,buf1,sizeof(buf1)); /*0 indica lo standard input*/&lt;br /&gt;
				write(efd, buf1, sizeof(buf1));&lt;br /&gt;
				}&lt;br /&gt;
			&lt;br /&gt;
		else{ /*figlio*/&lt;br /&gt;
			read(efd, buf2, sizeof(buf2));&lt;br /&gt;
			n=atoi(buf2);&lt;br /&gt;
			for(i=0; i&amp;lt;n; i++) printf(&amp;quot;x\n&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
GiuliaN. con grande aiuto da parte dei colleghi&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esercizio 1:(visto a lezione , soluzione del Prof. Davoli   Pirata_20131203)&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/eventfd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
#define BUFFSIZE 10&lt;br /&gt;
 &lt;br /&gt;
int main(int argc, char *argv[]){&lt;br /&gt;
	int efd , n , i;&lt;br /&gt;
	uint64_t a , b;&lt;br /&gt;
	if ( ( efd = eventfd( 0 , EFD_SEMAPHORE ) ) &amp;lt; 0 )&lt;br /&gt;
		exit( 1 );&lt;br /&gt;
	if ( fork() &amp;gt; 0 ){&lt;br /&gt;
		while ( 1 ) {&lt;br /&gt;
		uint64_t a;&lt;br /&gt;
		scanf( &amp;quot;%lld&amp;quot; , &amp;amp;a );&lt;br /&gt;
		write( efd , &amp;amp;a , sizeof( a ) );&lt;br /&gt;
				}&lt;br /&gt;
		}&lt;br /&gt;
	else {&lt;br /&gt;
		while ( 1 ) {&lt;br /&gt;
		uint64_t b;&lt;br /&gt;
		read( efd , &amp;amp;b , sizeof( b ) );&lt;br /&gt;
		printf( &amp;quot;x\n&amp;quot; );&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esercizio 3 in bash(Pirata &amp;amp; Fede):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#! /bin/bash&lt;br /&gt;
&lt;br /&gt;
for f in &amp;quot;$1&amp;quot;/*.* ; do&lt;br /&gt;
	somma=0&lt;br /&gt;
	ext=${f##*.}&lt;br /&gt;
	for file in *.&amp;quot;$ext&amp;quot;&lt;br /&gt;
	do&lt;br /&gt;
		somma=$(expr $somma + $(stat -c%s &amp;quot;$file&amp;quot;))&lt;br /&gt;
	done&lt;br /&gt;
	echo -n &amp;quot;size of file with extension &amp;quot;$ext&amp;quot; : &amp;quot;&lt;br /&gt;
	echo &amp;quot;$somma&amp;quot;&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Per (Pirata &amp;amp; Fede) -Bash-: ci siamo quasi...però forse rifate gli stessi calcoli un po' di volte in più del necessario ;-) (am_20131127).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[Esercizio 3 in Python (ancora)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
#&lt;br /&gt;
# Prova Pratica di Lab SO 2013.05.29&lt;br /&gt;
# Es. 3&lt;br /&gt;
# Eduardo Santarelli&lt;br /&gt;
#####################################&lt;br /&gt;
&lt;br /&gt;
import os, sys&lt;br /&gt;
&lt;br /&gt;
# return the suffix of file named fname, if present.&lt;br /&gt;
# return boolean value False otherwise&lt;br /&gt;
def get_suffix(fname):&lt;br /&gt;
	if '.' in fname:&lt;br /&gt;
		separator = fname.find('.')&lt;br /&gt;
		return fname[separator : ]&lt;br /&gt;
	else:&lt;br /&gt;
		return False	&lt;br /&gt;
&lt;br /&gt;
# Place a file in a dictionary. Each key corresponds&lt;br /&gt;
# to a specific suffix, its value is a list of all the&lt;br /&gt;
# file with that suffix&lt;br /&gt;
def build_dict(item, suffix):&lt;br /&gt;
	if suffix in all_files:&lt;br /&gt;
		all_files[suffix].append(item)&lt;br /&gt;
	else:&lt;br /&gt;
		all_files[suffix] = []&lt;br /&gt;
		all_files[suffix].append(item)&lt;br /&gt;
&lt;br /&gt;
# For each suffix key in the dictionary,&lt;br /&gt;
# calculates the total size of the files&lt;br /&gt;
# in the corresponding list&lt;br /&gt;
def size_of_entry(file_list):&lt;br /&gt;
		size = 0&lt;br /&gt;
		for item in file_list:&lt;br /&gt;
			size += os.path.getsize(item)&lt;br /&gt;
		return size&lt;br /&gt;
&lt;br /&gt;
# This dictionary will contain a key for each suffix,&lt;br /&gt;
# associated with a list of all the files ending with&lt;br /&gt;
# that suffix&lt;br /&gt;
all_files = dict()&lt;br /&gt;
&lt;br /&gt;
# Go to target directory and build a list containing all filenames&lt;br /&gt;
try:&lt;br /&gt;
	target_dir = sys.argv[1]&lt;br /&gt;
except:&lt;br /&gt;
	target_dir = '.'&lt;br /&gt;
os.chdir(target_dir)&lt;br /&gt;
dir_list = os.listdir('.')&lt;br /&gt;
&lt;br /&gt;
# For each file call get_suffix and then build_dict&lt;br /&gt;
# (unless get_suffix returns False)&lt;br /&gt;
for item in dir_list:&lt;br /&gt;
	suffix = get_suffix(item)&lt;br /&gt;
	if not suffix:&lt;br /&gt;
		pass&lt;br /&gt;
	else:&lt;br /&gt;
		build_dict(item, suffix)&lt;br /&gt;
&lt;br /&gt;
for key in all_files :&lt;br /&gt;
	print(&amp;quot;{0}: {1} Bytes&amp;quot;.format(key, size_of_entry(all_files[key])))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-Eduardo&lt;br /&gt;
--edit: dir_list = os.listdir(target_dir) non funzionava bene coi path relativi.&lt;br /&gt;
Ora os.listdir('.') dovrebbe andare bene.&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2013.05.29&amp;diff=379</id>
		<title>ProvaPratica 2013.05.29</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2013.05.29&amp;diff=379"/>
		<updated>2013-12-03T18:16:06Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[Python 3]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
Prova Pratica di Laboratorio di Sistemi Operativi&lt;br /&gt;
29 maggio 2013&lt;br /&gt;
Esercizio 3&lt;br /&gt;
&lt;br /&gt;
URL: http://www.cs.unibo.it/~renzo/so/pratiche/2013.05.29.pdf&lt;br /&gt;
&lt;br /&gt;
@author: Tommaso Ognibene&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
import os, sys&lt;br /&gt;
&lt;br /&gt;
def Main(argv):&lt;br /&gt;
    # Check number of arguments&lt;br /&gt;
    if len(argv) != 2:&lt;br /&gt;
        print(&amp;quot;The function requires one argument to be passed in.&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
    &lt;br /&gt;
    # Check parameters&lt;br /&gt;
    topDir = str(sys.argv[1])&lt;br /&gt;
    if not os.path.isdir(topDir):&lt;br /&gt;
        print(&amp;quot;The parameter should be an existing directory.&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
    &lt;br /&gt;
    # Build a dictionary with key-value pair {file extension - total size}&lt;br /&gt;
    extensionSize = { }&lt;br /&gt;
    GetSize(topDir, extensionSize)&lt;br /&gt;
    &lt;br /&gt;
    # Print results&lt;br /&gt;
    PrintResults(extensionSize)&lt;br /&gt;
&lt;br /&gt;
def GetSize(topDir, extensionSize):&lt;br /&gt;
    for dirPath, dirNames, files in os.walk(topDir):&lt;br /&gt;
        for file in files:&lt;br /&gt;
            # 'example.mp3' -&amp;gt; ['example', 'mp3']&lt;br /&gt;
            # 'example.tar.gz' -&amp;gt; ['example', 'tar', 'gz']&lt;br /&gt;
            parts = file.split('.')&lt;br /&gt;
            # ['example', 'mp3'] -&amp;gt; ['mp3']&lt;br /&gt;
            # ['example', 'tar', 'gz'] -&amp;gt; ['tar', 'gz']&lt;br /&gt;
            parts = parts[1:]&lt;br /&gt;
            # ['mp3'] -&amp;gt; '.mp3'&lt;br /&gt;
            # ['tar', 'gz'] -&amp;gt; '.tar.gz'&lt;br /&gt;
            fileExtension = &amp;quot;.{0}&amp;quot;.format(&amp;quot;.&amp;quot;.join(str(part) for part in parts))&lt;br /&gt;
            &lt;br /&gt;
            # Compute the size in Bytes and update the dictionary&lt;br /&gt;
            filePath = os.path.join(dirPath, file)   &lt;br /&gt;
            fileSize = os.path.getsize(filePath)&lt;br /&gt;
            extensionSize[fileExtension] = extensionSize.get(fileExtension, 0) + fileSize&lt;br /&gt;
&lt;br /&gt;
# Print results&lt;br /&gt;
def PrintResults(extensionSize):&lt;br /&gt;
    for key, value in sorted(extensionSize.items()):&lt;br /&gt;
        print('{0}: {1} Bytes.'.format(key, value))&lt;br /&gt;
        &lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    sys.exit(Main(sys.argv))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
mia versione&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
import os, sys, copy&lt;br /&gt;
&lt;br /&gt;
def dotsubstr(a):#restituisce la sottostringa .suffisso&lt;br /&gt;
	#fcd = os.listdir('{0}'.format(arg1))&lt;br /&gt;
	i=0&lt;br /&gt;
	try:&lt;br /&gt;
		while a[i]!='.':&lt;br /&gt;
			i=i+1&lt;br /&gt;
		return a[i:]&lt;br /&gt;
	except IndexError:&lt;br /&gt;
		return -1&lt;br /&gt;
&lt;br /&gt;
def compliarg(li,arg):#restituisce una lista di tutti gli elementi contenenti la sottostringa arg come suffisso&lt;br /&gt;
	res=[]&lt;br /&gt;
	while li != []:&lt;br /&gt;
		a=li.pop()&lt;br /&gt;
		if a.endswith(arg): res.append(a)&lt;br /&gt;
	return res&lt;br /&gt;
&lt;br /&gt;
def listremintsect(l2,l1):#restituisce una lista res = l1 - intersezione di l1 ed l2&lt;br /&gt;
	res=[]&lt;br /&gt;
	while l1 != []:&lt;br /&gt;
		a=l1.pop()&lt;br /&gt;
		if not a in l2: res.append(a)&lt;br /&gt;
	return res&lt;br /&gt;
&lt;br /&gt;
def createpathlist(c,path):#restituisce una lista di path 'path' relativi ai file contenuti in c.&lt;br /&gt;
	res = []&lt;br /&gt;
	while c != []:&lt;br /&gt;
		res.append('{0}/{1}'.format(path,c.pop()))&lt;br /&gt;
	return res&lt;br /&gt;
&lt;br /&gt;
def totsizes(d): #data una lista d di path restituisce la somma di tutti i size di d&lt;br /&gt;
	res = 0&lt;br /&gt;
	while d != []:&lt;br /&gt;
		a = d.pop()&lt;br /&gt;
		if os.path.isfile('{0}'.format(a)):&lt;br /&gt;
			res = res + os.path.getsize('{0}'.format(a))&lt;br /&gt;
	return res&lt;br /&gt;
&lt;br /&gt;
def listsubstr(arg): #ritorna un dizionario del tipo diz[str(suffisso)] = int(size relativo al suffisso)&lt;br /&gt;
	res = {}&lt;br /&gt;
	fcd = os.listdir('{0}'.format(arg))&lt;br /&gt;
	while fcd != []:&lt;br /&gt;
		fcdtmp=copy.deepcopy(fcd) #BUGGONE SENZA COPY!!!!!!&lt;br /&gt;
		a = fcd.pop()&lt;br /&gt;
		b = dotsubstr(a)&lt;br /&gt;
		if b == -1: continue&lt;br /&gt;
		else: pass&lt;br /&gt;
		c = compliarg(fcdtmp,b)&lt;br /&gt;
		s=copy.deepcopy(c) #!!!!!!!!!!!!!!!!!!!!!!!!!&lt;br /&gt;
		d = createpathlist(c,arg)&lt;br /&gt;
		res[b] = totsizes(d)&lt;br /&gt;
&lt;br /&gt;
		fcd = listremintsect(s,fcd)&lt;br /&gt;
&lt;br /&gt;
	return res&lt;br /&gt;
		&lt;br /&gt;
&lt;br /&gt;
try:&lt;br /&gt;
	res = listsubstr(sys.argv[1])&lt;br /&gt;
	a=list(res.keys())&lt;br /&gt;
	while a != []:&lt;br /&gt;
		b = a.pop()&lt;br /&gt;
		print('{0}:\t{1}'.format(b,res[b]))&lt;br /&gt;
except OSError:&lt;br /&gt;
	print(&amp;quot;Could not solve path&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-fede&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Esercizio 1: Linguaggio C&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/eventfd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 10&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]){&lt;br /&gt;
	int efd,n,i;&lt;br /&gt;
	char buf1[BUFFSIZE];&lt;br /&gt;
	char buf2[BUFFSIZE];&lt;br /&gt;
	&lt;br /&gt;
	do{&lt;br /&gt;
		efd = eventfd(0,0);&lt;br /&gt;
	}while(efd&amp;lt;0);&lt;br /&gt;
	&lt;br /&gt;
	while(1){&lt;br /&gt;
		if(fork()){ /*padre*/&lt;br /&gt;
				read(0,buf1,sizeof(buf1)); /*0 indica lo standard input*/&lt;br /&gt;
				write(efd, buf1, sizeof(buf1));&lt;br /&gt;
				}&lt;br /&gt;
			&lt;br /&gt;
		else{ /*figlio*/&lt;br /&gt;
			read(efd, buf2, sizeof(buf2));&lt;br /&gt;
			n=atoi(buf2);&lt;br /&gt;
			for(i=0; i&amp;lt;n; i++) printf(&amp;quot;x\n&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
GiuliaN. con grande aiuto da parte dei colleghi&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esercizio 1:(visto a lezione , soluzione del Prof. Davoli   Pirata_20131203)&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/eventfd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
#define BUFFSIZE 10&lt;br /&gt;
 &lt;br /&gt;
int main(int argc, char *argv[]){&lt;br /&gt;
	int efd , n , i;&lt;br /&gt;
	uint64_t a , b;&lt;br /&gt;
	if ( ( efd = eventfd( 0 , EFD_SEMAPHORE ) ) &amp;lt; 0 )&lt;br /&gt;
		exit( 1 );&lt;br /&gt;
	if ( fork() &amp;gt; 0 ){&lt;br /&gt;
		while ( 1 ) {&lt;br /&gt;
		uint64_t a;&lt;br /&gt;
		scanf( &amp;quot;%lld&amp;quot; , &amp;amp;a );&lt;br /&gt;
		write( efd , &amp;amp;a , sizeof( a ) );&lt;br /&gt;
				}&lt;br /&gt;
		}&lt;br /&gt;
	else {&lt;br /&gt;
		while ( 1 ) {&lt;br /&gt;
		uint64_t b;&lt;br /&gt;
		read( efd , &amp;amp;b , sizeof( b ) );&lt;br /&gt;
		//for ( i = 0 ; i &amp;lt; b ; i++ ) &lt;br /&gt;
		printf( &amp;quot;x\n&amp;quot; );&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esercizio 3 in bash(Pirata &amp;amp; Fede):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#! /bin/bash&lt;br /&gt;
&lt;br /&gt;
for f in &amp;quot;$1&amp;quot;/*.* ; do&lt;br /&gt;
	somma=0&lt;br /&gt;
	ext=${f##*.}&lt;br /&gt;
	for file in *.&amp;quot;$ext&amp;quot;&lt;br /&gt;
	do&lt;br /&gt;
		somma=$(expr $somma + $(stat -c%s &amp;quot;$file&amp;quot;))&lt;br /&gt;
	done&lt;br /&gt;
	echo -n &amp;quot;size of file with extension &amp;quot;$ext&amp;quot; : &amp;quot;&lt;br /&gt;
	echo &amp;quot;$somma&amp;quot;&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Per (Pirata &amp;amp; Fede) -Bash-: ci siamo quasi...però forse rifate gli stessi calcoli un po' di volte in più del necessario ;-) (am_20131127).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[Esercizio 3 in Python (ancora)]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
#&lt;br /&gt;
# Prova Pratica di Lab SO 2013.05.29&lt;br /&gt;
# Es. 3&lt;br /&gt;
# Eduardo Santarelli&lt;br /&gt;
#####################################&lt;br /&gt;
&lt;br /&gt;
import os, sys&lt;br /&gt;
&lt;br /&gt;
# return the suffix of file named fname, if present.&lt;br /&gt;
# return boolean value False otherwise&lt;br /&gt;
def get_suffix(fname):&lt;br /&gt;
	if '.' in fname:&lt;br /&gt;
		separator = fname.find('.')&lt;br /&gt;
		return fname[separator : ]&lt;br /&gt;
	else:&lt;br /&gt;
		return False	&lt;br /&gt;
&lt;br /&gt;
# Place a file in a dictionary. Each key corresponds&lt;br /&gt;
# to a specific suffix, its value is a list of all the&lt;br /&gt;
# file with that suffix&lt;br /&gt;
def build_dict(item, suffix):&lt;br /&gt;
	if suffix in all_files:&lt;br /&gt;
		all_files[suffix].append(item)&lt;br /&gt;
	else:&lt;br /&gt;
		all_files[suffix] = []&lt;br /&gt;
		all_files[suffix].append(item)&lt;br /&gt;
&lt;br /&gt;
# For each suffix key in the dictionary,&lt;br /&gt;
# calculates the total size of the files&lt;br /&gt;
# in the corresponding list&lt;br /&gt;
def size_of_entry(file_list):&lt;br /&gt;
		size = 0&lt;br /&gt;
		for item in file_list:&lt;br /&gt;
			size += os.path.getsize(item)&lt;br /&gt;
		return size&lt;br /&gt;
&lt;br /&gt;
# This dictionary will contain a key for each suffix,&lt;br /&gt;
# associated with a list of all the files ending with&lt;br /&gt;
# that suffix&lt;br /&gt;
all_files = dict()&lt;br /&gt;
&lt;br /&gt;
# Go to target directory and build a list containing all filenames&lt;br /&gt;
try:&lt;br /&gt;
	target_dir = sys.argv[1]&lt;br /&gt;
except:&lt;br /&gt;
	target_dir = '.'&lt;br /&gt;
os.chdir(target_dir)&lt;br /&gt;
dir_list = os.listdir('.')&lt;br /&gt;
&lt;br /&gt;
# For each file call get_suffix and then build_dict&lt;br /&gt;
# (unless get_suffix returns False)&lt;br /&gt;
for item in dir_list:&lt;br /&gt;
	suffix = get_suffix(item)&lt;br /&gt;
	if not suffix:&lt;br /&gt;
		pass&lt;br /&gt;
	else:&lt;br /&gt;
		build_dict(item, suffix)&lt;br /&gt;
&lt;br /&gt;
for key in all_files :&lt;br /&gt;
	print(&amp;quot;{0}: {1} Bytes&amp;quot;.format(key, size_of_entry(all_files[key])))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-Eduardo&lt;br /&gt;
--edit: dir_list = os.listdir(target_dir) non funzionava bene coi path relativi.&lt;br /&gt;
Ora os.listdir('.') dovrebbe andare bene.&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2013.05.29&amp;diff=361</id>
		<title>ProvaPratica 2013.05.29</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaPratica_2013.05.29&amp;diff=361"/>
		<updated>2013-11-26T22:42:02Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[Python 3]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
'''&lt;br /&gt;
Prova Pratica di Laboratorio di Sistemi Operativi&lt;br /&gt;
29 maggio 2013&lt;br /&gt;
Esercizio 3&lt;br /&gt;
&lt;br /&gt;
URL: http://www.cs.unibo.it/~renzo/so/pratiche/2013.05.29.pdf&lt;br /&gt;
&lt;br /&gt;
@author: Tommaso Ognibene&lt;br /&gt;
'''&lt;br /&gt;
&lt;br /&gt;
import os, sys&lt;br /&gt;
&lt;br /&gt;
def Main(argv):&lt;br /&gt;
    # Check number of arguments&lt;br /&gt;
    if len(argv) != 2:&lt;br /&gt;
        print(&amp;quot;The function requires one argument to be passed in.&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
    &lt;br /&gt;
    # Check parameters&lt;br /&gt;
    topDir = str(sys.argv[1])&lt;br /&gt;
    if not os.path.isdir(topDir):&lt;br /&gt;
        print(&amp;quot;The parameter should be an existing directory.&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
    &lt;br /&gt;
    # Build a dictionary with key-value pair {file extension - total size}&lt;br /&gt;
    extensionSize = { }&lt;br /&gt;
    GetSize(topDir, extensionSize)&lt;br /&gt;
    &lt;br /&gt;
    # Print results&lt;br /&gt;
    PrintResults(extensionSize)&lt;br /&gt;
&lt;br /&gt;
def GetSize(topDir, extensionSize):&lt;br /&gt;
    for dirPath, dirNames, files in os.walk(topDir):&lt;br /&gt;
        for file in files:&lt;br /&gt;
            # 'example.mp3' -&amp;gt; ['example', 'mp3']&lt;br /&gt;
            # 'example.tar.gz' -&amp;gt; ['example', 'tar', 'gz']&lt;br /&gt;
            parts = file.split('.')&lt;br /&gt;
            # ['example', 'mp3'] -&amp;gt; ['mp3']&lt;br /&gt;
            # ['example', 'tar', 'gz'] -&amp;gt; ['tar', 'gz']&lt;br /&gt;
            parts = parts[1:]&lt;br /&gt;
            # ['mp3'] -&amp;gt; '.mp3'&lt;br /&gt;
            # ['tar', 'gz'] -&amp;gt; '.tar.gz'&lt;br /&gt;
            fileExtension = &amp;quot;.{0}&amp;quot;.format(&amp;quot;.&amp;quot;.join(str(part) for part in parts))&lt;br /&gt;
            &lt;br /&gt;
            # Compute the size in Bytes and update the dictionary&lt;br /&gt;
            filePath = os.path.join(dirPath, file)   &lt;br /&gt;
            fileSize = os.path.getsize(filePath)&lt;br /&gt;
            extensionSize[fileExtension] = extensionSize.get(fileExtension, 0) + fileSize&lt;br /&gt;
&lt;br /&gt;
# Print results&lt;br /&gt;
def PrintResults(extensionSize):&lt;br /&gt;
    for key, value in sorted(extensionSize.items()):&lt;br /&gt;
        print('{0}: {1} Bytes.'.format(key, value))&lt;br /&gt;
        &lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    sys.exit(Main(sys.argv))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
mia versione&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Python&amp;quot;&amp;gt;&lt;br /&gt;
import os, sys, copy&lt;br /&gt;
&lt;br /&gt;
def dotsubstr(a):#restituisce la sottostringa .suffisso&lt;br /&gt;
	#fcd = os.listdir('{0}'.format(arg1))&lt;br /&gt;
	i=0&lt;br /&gt;
	try:&lt;br /&gt;
		while a[i]!='.':&lt;br /&gt;
			i=i+1&lt;br /&gt;
		return a[i:]&lt;br /&gt;
	except IndexError:&lt;br /&gt;
		return -1&lt;br /&gt;
&lt;br /&gt;
def compliarg(li,arg):#restituisce una lista di tutti gli elementi contenenti la sottostringa arg come suffisso&lt;br /&gt;
	res=[]&lt;br /&gt;
	while li != []:&lt;br /&gt;
		a=li.pop()&lt;br /&gt;
		if a.endswith(arg): res.append(a)&lt;br /&gt;
	return res&lt;br /&gt;
&lt;br /&gt;
def listremintsect(l2,l1):#restituisce una lista res = l1 - intersezione di l1 ed l2&lt;br /&gt;
	res=[]&lt;br /&gt;
	while l1 != []:&lt;br /&gt;
		a=l1.pop()&lt;br /&gt;
		if not a in l2: res.append(a)&lt;br /&gt;
	return res&lt;br /&gt;
&lt;br /&gt;
def createpathlist(c,path):#restituisce una lista di path 'path' relativi ai file contenuti in c.&lt;br /&gt;
	res = []&lt;br /&gt;
	while c != []:&lt;br /&gt;
		res.append('{0}/{1}'.format(path,c.pop()))&lt;br /&gt;
	return res&lt;br /&gt;
&lt;br /&gt;
def totsizes(d): #data una lista d di path restituisce la somma di tutti i size di d&lt;br /&gt;
	res = 0&lt;br /&gt;
	while d != []:&lt;br /&gt;
		a = d.pop()&lt;br /&gt;
		if os.path.isfile('{0}'.format(a)):&lt;br /&gt;
			res = res + os.path.getsize('{0}'.format(a))&lt;br /&gt;
	return res&lt;br /&gt;
&lt;br /&gt;
def listsubstr(arg): #ritorna un dizionario del tipo diz[str(suffisso)] = int(size relativo al suffisso)&lt;br /&gt;
	res = {}&lt;br /&gt;
	fcd = os.listdir('{0}'.format(arg))&lt;br /&gt;
	while fcd != []:&lt;br /&gt;
		fcdtmp=copy.deepcopy(fcd) #BUGGONE SENZA COPY!!!!!!&lt;br /&gt;
		a = fcd.pop()&lt;br /&gt;
		b = dotsubstr(a)&lt;br /&gt;
		if b == -1: continue&lt;br /&gt;
		else: pass&lt;br /&gt;
		c = compliarg(fcdtmp,b)&lt;br /&gt;
		s=copy.deepcopy(c) #!!!!!!!!!!!!!!!!!!!!!!!!!&lt;br /&gt;
		d = createpathlist(c,arg)&lt;br /&gt;
		res[b] = totsizes(d)&lt;br /&gt;
&lt;br /&gt;
		fcd = listremintsect(s,fcd)&lt;br /&gt;
&lt;br /&gt;
	return res&lt;br /&gt;
		&lt;br /&gt;
&lt;br /&gt;
try:&lt;br /&gt;
	res = listsubstr(sys.argv[1])&lt;br /&gt;
	a=list(res.keys())&lt;br /&gt;
	while a != []:&lt;br /&gt;
		b = a.pop()&lt;br /&gt;
		print('{0}:\t{1}'.format(b,res[b]))&lt;br /&gt;
except OSError:&lt;br /&gt;
	print(&amp;quot;Could not solve path&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-fede&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Esercizio 1: Linguaggio C&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/eventfd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 10&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]){&lt;br /&gt;
	int efd,n,i;&lt;br /&gt;
	char buf1[BUFFSIZE];&lt;br /&gt;
	char buf2[BUFFSIZE];&lt;br /&gt;
	&lt;br /&gt;
	do{&lt;br /&gt;
		efd = eventfd(0,0);&lt;br /&gt;
	}while(efd&amp;lt;0);&lt;br /&gt;
	&lt;br /&gt;
	while(1){&lt;br /&gt;
		if(fork()){ /*padre*/&lt;br /&gt;
				read(0,buf1,sizeof(buf1)); /*0 indica lo standard input*/&lt;br /&gt;
				write(efd, buf1, sizeof(buf1));&lt;br /&gt;
				}&lt;br /&gt;
			&lt;br /&gt;
		else{ /*figlio*/&lt;br /&gt;
			read(efd, buf2, sizeof(buf2));&lt;br /&gt;
			n=atoi(buf2);&lt;br /&gt;
			for(i=0; i&amp;lt;n; i++) printf(&amp;quot;x\n&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
GiuliaN. con grande aiuto da parte dei colleghi&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esercizio 3 in bash(Pirata &amp;amp; Fede):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#! /bin/bash&lt;br /&gt;
&lt;br /&gt;
for f in *.*&lt;br /&gt;
do&lt;br /&gt;
	somma=0&lt;br /&gt;
	ext=${f##*.}&lt;br /&gt;
	for file in *.$ext&lt;br /&gt;
	do&lt;br /&gt;
		somma=$(expr $somma + $(stat -c%s &amp;quot;$file&amp;quot;))&lt;br /&gt;
	done&lt;br /&gt;
	echo -n &amp;quot;size of file with extension $ext : &amp;quot;&lt;br /&gt;
	echo &amp;quot;$somma&amp;quot;&lt;br /&gt;
done&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=329</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=329"/>
		<updated>2013-11-24T15:55:23Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* DIRECTORY */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===LSEEK/READ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Read: legge da un file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio3(copia di uno sparsify file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio4(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
Esempio(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===DUP===&lt;br /&gt;
&lt;br /&gt;
*Dup: duplica il file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[]){&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	close(1);&lt;br /&gt;
	dup(fd);   //dup2(fd,1) produce lo stesso effetto&lt;br /&gt;
	printf(&amp;quot;Hello World \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===FUNZIONI SULLE DIRECTORY===&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int mystrcmp( const void *a , const void *b ){&lt;br /&gt;
	char **pa = (char **)a;&lt;br /&gt;
	char **pb = (char **)b;&lt;br /&gt;
	return strcmp( *pa , *pb );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	DIR *d;&lt;br /&gt;
	struct dirent *de;&lt;br /&gt;
	char **names;&lt;br /&gt;
	int i , count = 0;&lt;br /&gt;
	d = opendir( argv[1] );     &lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		count++;&lt;br /&gt;
	rewinddir(d);&lt;br /&gt;
	names = (char **)calloc( count , sizeof(char *) );&lt;br /&gt;
	count=0;&lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		names[count++] = strdup( de-&amp;gt;d_name );&lt;br /&gt;
	closedir(d);&lt;br /&gt;
	qsort( names , count , sizeof(char *) , mystrcmp );&lt;br /&gt;
	for( i = 0 ; i &amp;lt; count ; i++ )&lt;br /&gt;
		printf(&amp;quot; %s &amp;quot;, names[i] );&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include&amp;lt;string.h&amp;gt;&lt;br /&gt;
#include&amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int count;&lt;br /&gt;
	int i;&lt;br /&gt;
	struct dirent **de;&lt;br /&gt;
	count = scandir( argv[1] , &amp;amp;de , NULL , alphasort );&lt;br /&gt;
	for ( i = 0 ; i &amp;lt; count ; i++ ){&lt;br /&gt;
		printf(&amp;quot;%s &amp;quot;,de[i]-&amp;gt;d_name);&lt;br /&gt;
		free(de[i]);&lt;br /&gt;
	}&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	free(de);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=328</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=328"/>
		<updated>2013-11-24T15:54:37Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* DIRECTORY */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===LSEEK/READ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Read: legge da un file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio3(copia di uno sparsify file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio4(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
Esempio(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===DUP===&lt;br /&gt;
&lt;br /&gt;
*Dup: duplica il file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[]){&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	close(1);&lt;br /&gt;
	dup(fd);   //dup2(fd,1) produce lo stesso effetto&lt;br /&gt;
	printf(&amp;quot;Hello World \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===DIRECTORY===&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int mystrcmp( const void *a , const void *b ){&lt;br /&gt;
	char **pa = (char **)a;&lt;br /&gt;
	char **pb = (char **)b;&lt;br /&gt;
	return strcmp( *pa , *pb );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	DIR *d;&lt;br /&gt;
	struct dirent *de;&lt;br /&gt;
	char **names;&lt;br /&gt;
	int i , count = 0;&lt;br /&gt;
	d = opendir( argv[1] );     &lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		count++;&lt;br /&gt;
	rewinddir(d);&lt;br /&gt;
	names = (char **)calloc( count , sizeof(char *) );&lt;br /&gt;
	count=0;&lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		names[count++] = strdup( de-&amp;gt;d_name );&lt;br /&gt;
	closedir(d);&lt;br /&gt;
	qsort( names , count , sizeof(char *) , mystrcmp );&lt;br /&gt;
	for( i = 0 ; i &amp;lt; count ; i++ )&lt;br /&gt;
		printf(&amp;quot; %s &amp;quot;, names[i] );&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include&amp;lt;string.h&amp;gt;&lt;br /&gt;
#include&amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ){&lt;br /&gt;
	int count;&lt;br /&gt;
	int i;&lt;br /&gt;
	struct dirent **de;&lt;br /&gt;
	count = scandir( argv[1] , &amp;amp;de , NULL , alphasort );&lt;br /&gt;
	for ( i = 0 ; i &amp;lt; count ; i++ ){&lt;br /&gt;
		printf(&amp;quot;%s &amp;quot;,de[i]-&amp;gt;d_name);&lt;br /&gt;
		free(de[i]);&lt;br /&gt;
	}&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	free(de);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=326</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=326"/>
		<updated>2013-11-24T15:20:12Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* DUP */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===LSEEK/READ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Read: legge da un file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio3(copia di uno sparsify file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio4(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
Esempio(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===DUP===&lt;br /&gt;
&lt;br /&gt;
*Dup: duplica il file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[]){&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	close(1);&lt;br /&gt;
	dup(fd);   //dup2(fd,1) produce lo stesso effetto&lt;br /&gt;
	printf(&amp;quot;Hello World \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===DIRECTORY===&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int mystrcmp( const void *a , const void *b ){&lt;br /&gt;
	char **pa = (char **)a;&lt;br /&gt;
	char **pb = (char **)b;&lt;br /&gt;
	return strcmp( *pa , *pb );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	DIR *d;&lt;br /&gt;
	struct dirent *de;&lt;br /&gt;
	char **names;&lt;br /&gt;
	int i , count = 0;&lt;br /&gt;
	d = opendir( argv[1] );     &lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		count++;&lt;br /&gt;
	rewinddir(d);&lt;br /&gt;
	names = (char **)calloc( count , sizeof(char *) );&lt;br /&gt;
	count=0;&lt;br /&gt;
	while( ( de = readdir(d) ) != NULL )&lt;br /&gt;
		names[count++] = strdup( de-&amp;gt;d_name );&lt;br /&gt;
	closedir(d);&lt;br /&gt;
	qsort( names , count , sizeof(char *) , mystrcmp );&lt;br /&gt;
	for( i = 0 ; i &amp;lt; count ; i++ )&lt;br /&gt;
		printf(&amp;quot; %s &amp;quot;, names[i] );&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=324</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=324"/>
		<updated>2013-11-24T14:31:19Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* LSEEK/READ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===LSEEK/READ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Read: legge da un file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio3(copia di uno sparsify file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio4(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
Esempio(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===DUP===&lt;br /&gt;
&lt;br /&gt;
*Dup: duplica il file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[]){&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	close(1);&lt;br /&gt;
	dup(fd);   //dup2(fd,1) produce lo stesso effetto&lt;br /&gt;
	printf(&amp;quot;Hello World \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=323</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=323"/>
		<updated>2013-11-24T14:30:43Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* LSEEK/READ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===LSEEK/READ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Read: legge da un file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio1(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio3(copia di uno sparsify file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio4(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
Esempio(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===DUP===&lt;br /&gt;
&lt;br /&gt;
*Dup: duplica il file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[]){&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	close(1);&lt;br /&gt;
	dup(fd);   //dup2(fd,1) produce lo stesso effetto&lt;br /&gt;
	printf(&amp;quot;Hello World \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=321</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=321"/>
		<updated>2013-11-24T00:41:39Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* DUP */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===LSEEK/READ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Read: legge da un file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio1(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio3(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
Esempio(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===DUP===&lt;br /&gt;
&lt;br /&gt;
*Dup: duplica il file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[]){&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	close(1);&lt;br /&gt;
	dup(fd);   //dup2(fd,1) produce lo stesso effetto&lt;br /&gt;
	printf(&amp;quot;Hello World \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=320</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=320"/>
		<updated>2013-11-24T00:39:27Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* LSEEK */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===LSEEK/READ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Read: legge da un file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio1(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio3(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
Esempio(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===DUP===&lt;br /&gt;
&lt;br /&gt;
*Dup: duplica il file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[]){&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	close(1);&lt;br /&gt;
	dup(fd);&lt;br /&gt;
	printf(&amp;quot;Hello World \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=319</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=319"/>
		<updated>2013-11-24T00:37:47Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* OPEN/WRITE/CLOSE/LSEEK/TRUNCATE */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===LSEEK===&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio1(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio2(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Esempio3(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
Esempio(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===DUP===&lt;br /&gt;
&lt;br /&gt;
*Dup: duplica il file descriptor.&lt;br /&gt;
&lt;br /&gt;
Esempio:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[]){&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	close(1);&lt;br /&gt;
	dup(fd);&lt;br /&gt;
	printf(&amp;quot;Hello World \n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=318</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=318"/>
		<updated>2013-11-23T23:48:01Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* OPEN/WRITE/CLOSE/LSEEK/TRUNCATE */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE/LSEEK/TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio3(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio4(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio5(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=317</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=317"/>
		<updated>2013-11-23T23:47:12Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE/LSEEK/TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio3(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio4(Buffer ed efficienza):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*eseguire lo script di cui sotto in modo da osservare come il buffer modifichi l'efficienza della copia per blocchi di byte&lt;br /&gt;
&lt;br /&gt;
dd if=/dev/urandom  of=casual  bs=1024  count=102400&lt;br /&gt;
for size in 128 256 512 1024 2048 4096&lt;br /&gt;
do &lt;br /&gt;
time ./eseguibile casual ricasual $size&lt;br /&gt;
done&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	int buffsize = atoi( argv[3] );&lt;br /&gt;
	char buff[buffsize];&lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , buffsize ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio5(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=316</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=316"/>
		<updated>2013-11-23T23:30:59Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* OPEN/WRITE/CLOSE/LSEEK/TRUNCATE */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE/LSEEK/TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio3(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio4(viene usata per creare File Buco Nero):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=297</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=297"/>
		<updated>2013-11-23T13:11:31Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* OPEN/WRITE/CLOSE/LSEEK/TRUNCATE */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE/LSEEK/TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
*Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
*Write: scrive su il file descriptor.&lt;br /&gt;
*Close: chiude il file descriptor.&lt;br /&gt;
*Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
*Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio3(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio4:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=296</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=296"/>
		<updated>2013-11-23T13:10:46Z</updated>

		<summary type="html">&lt;p&gt;Pirata: /* OPEN/WRITE/CLOSE/LSEEK/TRUNCATE */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE/LSEEK/TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
Write: scrive su il file descriptor.&lt;br /&gt;
Close: chiude il file descriptor.&lt;br /&gt;
Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio3(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio4:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=295</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=295"/>
		<updated>2013-11-23T13:09:51Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE/LSEEK/TRUNCATE===&lt;br /&gt;
&lt;br /&gt;
Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
Write: scrive su il file descriptor.&lt;br /&gt;
Close: chiude il file descriptor.&lt;br /&gt;
Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
Truncate: tronca il file a una specifica lunghezza.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio3(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio4:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main( int argc , char *argv[] ) {&lt;br /&gt;
	int fd = open( argv[1] , O_WRONLY | O_CREAT | O_TRUNC , 0666 );&lt;br /&gt;
	ftruncate( fd , atoi( argv[2] ) );&lt;br /&gt;
	close( fd );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=277</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=277"/>
		<updated>2013-11-21T23:14:44Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE/LSEEK===&lt;br /&gt;
&lt;br /&gt;
Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
Write: scrive su il file descriptor.&lt;br /&gt;
Close: chiude il file descriptor.&lt;br /&gt;
Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio3(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=276</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=276"/>
		<updated>2013-11-21T23:12:23Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE/LSEEK===&lt;br /&gt;
&lt;br /&gt;
Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
Write: scrive su il file descriptor.&lt;br /&gt;
Close: chiude il file descriptor.&lt;br /&gt;
Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio3(copia di un file):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define BUFFSIZE 1024             //la copia avviene per blocchi di 1024B&lt;br /&gt;
&lt;br /&gt;
char buff[BUFFSIZE];&lt;br /&gt;
&lt;br /&gt;
int isEmpty( char *s , int l ){&lt;br /&gt;
	int i;&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) return (0);&lt;br /&gt;
	return(1);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# if 0&lt;br /&gt;
	for ( i = 0 ; ( i &amp;lt; l ) &amp;amp;&amp;amp; ( s[i] != 0 ) ; i++ )&lt;br /&gt;
		;&lt;br /&gt;
	return ( i == l );             //codice più compatto &lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
	for( i = 0 ; i &amp;lt; l ; i++ )&lt;br /&gt;
		if( s[i] != 0 ) goto nonempty;&lt;br /&gt;
	return1;&lt;br /&gt;
nonempty:&lt;br /&gt;
	return0;                        //in stile kernel &lt;br /&gt;
#endif&lt;br /&gt;
}		&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fin = open( argv[1] , O_RDONLY );&lt;br /&gt;
	int fout = open( argv[2] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	ssize_t n;&lt;br /&gt;
	while( ( n = read( fin , buff , BUFFSIZE ) ) != 0 ){&lt;br /&gt;
		if (isEmpty( buff , n ) )                       //il codice in questo esempio é ottimizzato per gli sparsify file.&lt;br /&gt;
			lseek( fout , n , SEEK_CUR );&lt;br /&gt;
		else&lt;br /&gt;
		write( fout , buff , n );&lt;br /&gt;
	}&lt;br /&gt;
	close( fin);&lt;br /&gt;
	close( fout);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=272</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=272"/>
		<updated>2013-11-21T22:15:03Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE/LSEEK===&lt;br /&gt;
&lt;br /&gt;
Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
Write: scrive su il file descriptor.&lt;br /&gt;
Close: chiude il file descriptor.&lt;br /&gt;
Lseek: sposta l'offset del file descriptor.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2(sparsify file o file ciambella):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	lseek( fd, 1000000 , SEEK_SET );&lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=271</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=271"/>
		<updated>2013-11-21T22:06:08Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE/LSEEK===&lt;br /&gt;
&lt;br /&gt;
Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
Write: scrive su il file descriptor passato.&lt;br /&gt;
Close: chiude il file descriptor passato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=270</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=270"/>
		<updated>2013-11-21T22:00:10Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Controllo processi==&lt;br /&gt;
===FORK===&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===_EXIT===&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===WAIT===&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===EXEC===&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exec() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;br /&gt;
&lt;br /&gt;
==Gestione File==&lt;br /&gt;
* int open(const char *path, int oflag, ...);&amp;lt;br/&amp;gt;&lt;br /&gt;
* int creat(const char *path, mode_t mode);&lt;br /&gt;
* int close(int filedes);&lt;br /&gt;
* off_t lseek(int filedes, off_t offset, int whence);&lt;br /&gt;
* ssize_t read(int filedes, void *buf, size_t nbyte);&lt;br /&gt;
* ssize_t write(int filedes, const void *buf, size_t nbyte);&lt;br /&gt;
&lt;br /&gt;
Qualche esempio? Iniziamo con i file &amp;quot;con il buco&amp;quot; visti a lezione?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===OPEN/WRITE/CLOSE===&lt;br /&gt;
&lt;br /&gt;
Open: apre e possibilmente crea un file o un device.&lt;br /&gt;
Write: scrive su il file descriptor passato.&lt;br /&gt;
Close: chiude il file descriptor passato.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv[] ){&lt;br /&gt;
	int fd = open( argv[1] , O_RDWR | O_CREAT | O_TRUNC ,0666); &lt;br /&gt;
	write( fd , argv[2] , strlen( argv[2] ) );&lt;br /&gt;
	close( fd);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=185</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=185"/>
		<updated>2013-11-10T13:50:00Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* FORK&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*_EXIT&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* WAIT&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*EXECVE&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exe() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Funzione_con_numero_variabile_di_parametri.&amp;diff=184</id>
		<title>Funzione con numero variabile di parametri.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Funzione_con_numero_variabile_di_parametri.&amp;diff=184"/>
		<updated>2013-11-10T13:49:17Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Esempio di funzione che prende in argomento un numero variable di parametri:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdarg.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void foo(int narg, ... )   //narg é il numero di argomenti che verranno passati&lt;br /&gt;
{&lt;br /&gt;
	int i;&lt;br /&gt;
	va_list  ap; //Il tipo di dato va_list é una lista di variabili&lt;br /&gt;
	va_start(ap,narg); // Macro che inizializza ap&lt;br /&gt;
	for ( i = 0 ; i &amp;lt; narg ; i++) {&lt;br /&gt;
	int this = va_arg( ap , int ); //Macro che dice il tipo e il valore che deve&lt;br /&gt;
	printf(&amp;quot; %d &amp;quot;, this);         //avere il prossimo argomento della chiamata&lt;br /&gt;
	}&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	va_end(ap);   //Macro che chiude la macro di inizializzazione &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv)&lt;br /&gt;
{&lt;br /&gt;
	foo( 3 , 1 , 2 , 3 );&lt;br /&gt;
	foo( 4 , 0 , 54 , 32 , 88 );&lt;br /&gt;
	foo( 2 , 3 , 4 );&lt;br /&gt;
	foo( 0 );&lt;br /&gt;
	foo( 1 , 42 );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
-Pirata-&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Funzione_con_numero_variabile_di_parametri.&amp;diff=183</id>
		<title>Funzione con numero variabile di parametri.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Funzione_con_numero_variabile_di_parametri.&amp;diff=183"/>
		<updated>2013-11-10T13:48:27Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Esempio di funzione che prende in argomento un numero variable di parametri:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdarg.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void foo(int narg, ... )   //narg é il numero di argomenti che verranno passati&lt;br /&gt;
{&lt;br /&gt;
	int i;&lt;br /&gt;
	va_list  ap; //Il tipo di dato va_list é una lista di variabili&lt;br /&gt;
	va_start(ap,narg); // Macro che inizializza ap&lt;br /&gt;
	for ( i = 0 ; i &amp;lt; narg ; i++) {&lt;br /&gt;
	int this = va_arg( ap , int ); //Macro che dice il tipo e il valore che deve&lt;br /&gt;
	printf(&amp;quot; %d &amp;quot;, this);         //avere il prossimo argomento della chiamata&lt;br /&gt;
	}&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	va_end(ap);   //Macro che chiude la macro di inizializzazione &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv)&lt;br /&gt;
{&lt;br /&gt;
	foo( 3 , 1 , 2 , 3 );&lt;br /&gt;
	foo( 4 , 0 , 54 , 32 , 88 );&lt;br /&gt;
	foo( 2 , 3 , 4 );&lt;br /&gt;
	foo( 0 );&lt;br /&gt;
	foo( 1 , 42 );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Funzione_con_numero_variabile_di_parametri.&amp;diff=182</id>
		<title>Funzione con numero variabile di parametri.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Funzione_con_numero_variabile_di_parametri.&amp;diff=182"/>
		<updated>2013-11-10T13:47:21Z</updated>

		<summary type="html">&lt;p&gt;Pirata: Created page with &amp;quot;Esempio di funzione che prende in argomento un numero variable di parametri:  &amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt; #include &amp;lt;stdio.h&amp;gt; #include &amp;lt;stdarg.h&amp;gt;  void foo(int narg, ... ) { 	in...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Esempio di funzione che prende in argomento un numero variable di parametri:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdarg.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void foo(int narg, ... )&lt;br /&gt;
{&lt;br /&gt;
	int i;&lt;br /&gt;
	va_list  ap; //Il tipo di dato va_list é una lista di variabili&lt;br /&gt;
	va_start(ap,narg); // Macro che inizializza ap&lt;br /&gt;
	for ( i = 0 ; i &amp;lt; narg ; i++) {&lt;br /&gt;
	int this = va_arg( ap , int ); //Macro che dice il tipo e il valore che deve&lt;br /&gt;
	printf(&amp;quot; %d &amp;quot;, this);         //avere il prossimo argomento della chiamata&lt;br /&gt;
	}&lt;br /&gt;
	printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
	va_end(ap);   //Macro che chiude la macro di inizializzazione &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main ( int argc , char *argv)&lt;br /&gt;
{&lt;br /&gt;
	foo( 3 , 1 , 2 , 3 );&lt;br /&gt;
	foo( 4 , 0 , 54 , 32 , 88 );&lt;br /&gt;
	foo( 2 , 3 , 4 );&lt;br /&gt;
	foo( 0 );&lt;br /&gt;
	foo( 1 , 42 );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Main_Page&amp;diff=181</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Main_Page&amp;diff=181"/>
		<updated>2013-11-10T13:43:16Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Questo &amp;amp;egrave; il Wiki del Corso di Sistemi Operativi&lt;br /&gt;
&lt;br /&gt;
[[Esercizi a caso del Prof.]]&lt;br /&gt;
&lt;br /&gt;
[[Python Programma tieni punteggio.]]&lt;br /&gt;
&lt;br /&gt;
[[Comandi visti alle lezioni.]]&lt;br /&gt;
&lt;br /&gt;
[[SYS CALL viste a lezione.]]&lt;br /&gt;
&lt;br /&gt;
[[Parametri con getopt().]]&lt;br /&gt;
&lt;br /&gt;
[[Funzione con numero variabile di parametri.]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
Ricordate che per creare un account o quando viene richiesto di risolvere un semplice calcolo occorre ricordare quanto scritto [[qui]]&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=180</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=180"/>
		<updated>2013-11-10T11:03:08Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* FORK&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*_EXIT&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* WAIT&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*EXECVE&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exe() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d %s \n&amp;quot;,getpid(),pid,argv[1]);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,&amp;amp;status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		execvp(argv[2],argv+2);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
(Pirata)&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=179</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=179"/>
		<updated>2013-11-10T10:47:33Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* FORK&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*_EXIT&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* WAIT&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*EXECVE&lt;br /&gt;
Esegue un programma.&lt;br /&gt;
La famiglia di funzioni exe() rimpiazza l'immagine del processo corrente con l'immagine di un nuovo processo.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,status,0);&lt;br /&gt;
		if (WIFEXITED(status)) &lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if( WIFSIGNALED(status) )  &lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) ); &lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		char *args[] = {&amp;quot;ls&amp;quot;,&amp;quot;-l&amp;quot;,(char *)0};&lt;br /&gt;
		execvp(&amp;quot;ls&amp;quot;,args); //funzione di libreria che prende in input il path(p) e un vettore(v).&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
(Pirata)&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=178</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=178"/>
		<updated>2013-11-10T10:22:52Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* FORK&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*_EXIT&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* WAIT&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //Macro che restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
(Pirata)&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=177</id>
		<title>SYS CALL viste a lezione.</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=SYS_CALL_viste_a_lezione.&amp;diff=177"/>
		<updated>2013-11-10T10:04:50Z</updated>

		<summary type="html">&lt;p&gt;Pirata: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* FORK&lt;br /&gt;
Crea un nuovo processo duplicando il processo chiamante. Il nuovo processo , chiamato figlio, é un duplicato esatto del processo chiamante , chiamato padre. &lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){     &lt;br /&gt;
	if(fork()){&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() ); // getpid() stampa il pid del processo corrente , getppid() stampa il pid del processo padre&lt;br /&gt;
        sleep(2);}   //se togliessimo lo sleep;il processo figlio stamperebbe come getppid() 1,perché essendo il padre terminato e lui rimasto orfano,il nuovo padre diventa init&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(una piccola osservazione sull'eredità del buffer):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	printf(&amp;quot;cucù! : &amp;quot;);      //notare la differenza se nella sringa metto \n &lt;br /&gt;
	if(fork())&lt;br /&gt;
	printf(&amp;quot;uno  %d  %d \n&amp;quot;, getpid(), getppid() );&lt;br /&gt;
	else&lt;br /&gt;
	printf(&amp;quot;due %d %d  \n&amp;quot;,getpid(), getppid() );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio3(fork e malloc):&amp;lt;br/&amp;gt;&lt;br /&gt;
'''(sottolineerei che lo spazio di indirizzamento è di fatto separato: i due processi hanno una analoga visione dell'indirizzamento logico, che però è specifico di ogni processo e i dati sono memorizzati fisicamente in aree di memoria diverse. am_20131110)'''&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void main (void){&lt;br /&gt;
	int *p;&lt;br /&gt;
	if( fork() ){&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	*p = 45;&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());   &lt;br /&gt;
	sleep(2);&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
	p = malloc(sizeof(int));&lt;br /&gt;
	printf(&amp;quot;%d %p  pid : %d  , ppid : %d \n&amp;quot;,*p,p,getpid(),getppid());  //i puntatori restituiranno lo stesso indirizzo di memoria sia per il padre che per il figlio         &lt;br /&gt;
	sleep(1);                                                           //perché condividono lo stesso spazio di indirizzamento&lt;br /&gt;
	}&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*_EXIT&lt;br /&gt;
Termina il proceso chiamante.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	_exit(0);                     //Sys call che termina il processo chiamante&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
esempio2(atexit):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void at1(void){&lt;br /&gt;
	printf(&amp;quot;at1 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
void at2(void){&lt;br /&gt;
	printf(&amp;quot;at2 \n&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main (int argc,char *argv[]){&lt;br /&gt;
	printf(&amp;quot;brutal kill \n&amp;quot;);&lt;br /&gt;
	atexit(at1);	//funzione di libreria che termina il processo passato come argomento&lt;br /&gt;
	atexit(at2); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//le funzioni passate ad atexit vengono eseguite in modo inverso rispetto all'invocazione&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* WAIT&lt;br /&gt;
Aspetta che un processo cambi di stato.&lt;br /&gt;
&lt;br /&gt;
esempio1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
                sleep(10); &lt;br /&gt;
		waitpid(pid,status,0);&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;,WEXITSTATUS(status)); //Macro che restituisce l' exit status del figlio,cioè gli otto bit meno significativi dello status che il figlio&lt;br /&gt;
	}                                                         //specifica in una chiamata di _exit o exit o come argomento di ritorno del main.&lt;br /&gt;
	else {&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		exit(1);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
esempio2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/wait.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main (int argc, char *argv[]){&lt;br /&gt;
	pid_t pid;&lt;br /&gt;
	if( pid = fork() ){&lt;br /&gt;
		int status;&lt;br /&gt;
		printf(&amp;quot; padre : %d  figlio : %d \n&amp;quot;,getpid(),pid);&lt;br /&gt;
		sleep(5);&lt;br /&gt;
		waitpid(pid,status,0);&lt;br /&gt;
		if (WIFEXITED(status)) //Macro che restituisce true se il figlio termina normalmente, cioé chiamando _exit o exit oppure tramite il return del main&lt;br /&gt;
		printf(&amp;quot; exit status %d \n&amp;quot;, WEXITSTATUS(status));&lt;br /&gt;
		else if(WIFSIGNALED(status) )  //Macro che restituisce true se il figlio é terminato da un segnale&lt;br /&gt;
			printf(&amp;quot;signal : %d \n&amp;quot;, WTERMSIG(status) );  //restituisce il numero del segnale che causa la terminazione del processo figlio&lt;br /&gt;
	}&lt;br /&gt;
	else {&lt;br /&gt;
		int *p;&lt;br /&gt;
		printf(&amp;quot; figlio : %d  padre : %d \n&amp;quot;,getpid(),getppid());&lt;br /&gt;
		sleep(1);&lt;br /&gt;
		p = (int *)42;&lt;br /&gt;
		*p = 43;	&lt;br /&gt;
		exit(2);&lt;br /&gt;
		}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Correggetemi se ho scritto delle sciocchezze.&lt;br /&gt;
(Pirata)&lt;/div&gt;</summary>
		<author><name>Pirata</name></author>
	</entry>
</feed>