Difference between revisions of "Esempi del 02 dicembre 2014"

From Sistemi Operativi
Jump to navigation Jump to search
 
(2 intermediate revisions by one other user not shown)
Line 2: Line 2:
  
 
<source lang="c">
 
<source lang="c">
#include <stdlib.h>
 
 
#include <stdio.h>
 
#include <stdio.h>
#include <sys/types.h>
 
 
#include <unistd.h>
 
#include <unistd.h>
#include <sys/stat.h>
+
#include <stdlib.h>
 
#include <fcntl.h>
 
#include <fcntl.h>
  
Line 36: Line 34:
 
   lseek(file, MILIONE, SEEK_SET);
 
   lseek(file, MILIONE, SEEK_SET);
 
   write(file, str, 5);
 
   write(file, str, 5);
 +
  exit(0);
 +
}
 +
</source>
 +
 +
Test dup2
 +
 +
<source lang="c">
 +
#include <stdio.h>
 +
#include <fcntl.h>
 +
#include <unistd.h>
 +
#include <stdlib.h>
 +
 +
int main(int argc, char* argv[])
 +
{
 +
  int file;
 +
  file = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC);
 +
 +
  dup2(file, 1);
 +
  printf("ciao\n");
 
   exit(0);
 
   exit(0);
 
}
 
}
 
</source>
 
</source>

Latest revision as of 17:39, 2 December 2014

scrivere un programma C che faccia lseek a 1miliardo (SEEK_SET) e scriva "ciao".

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

int main( int argc, char* argv[]){
	int f;
	f = open( argv[1] , O_WRONLY | O_CREAT );
	lseek( f , 1000000000 , SEEK_SET ) ;
	write( f , "ciao" , 4 ) ;
	return 0 ;
}

Non sono effettivamente riuscito ad aprire il file creato, ma ha la dimensione di 1 Gigabyte.

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#define MILIONE 1000000

int main(int argc, char* argv[])
{
   int file;
   file = open("./test.txt", O_WRONLY | O_CREAT | O_TRUNC);

   char* str = "ciao";

   lseek(file, MILIONE, SEEK_SET);
   write(file, str, 5);
   exit(0);
}

Test dup2

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
   int file;
   file = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC);

   dup2(file, 1);
   printf("ciao\n");
   exit(0);
}