Difference between revisions of "ProvaPratica 2013.02.15"

From Sistemi Operativi
Jump to navigation Jump to search
(Created page with "Esercizio 1 del 2013.02.15 <syntaxhighlight lang="C"> /* * Prova Pratica di Lab SO 2013.02.15 * http://www.cs.unibo.it/~renzo/so/pratiche/2013.02.15.pdf * Es.1 * Eduardo S...")
 
Line 2: Line 2:
 
<syntaxhighlight lang="C">
 
<syntaxhighlight lang="C">
 
/*
 
/*
  * Prova Pratica di Lab SO 2013.02.15
+
  * Prova Pratica di Lab SO 2013.02.15
  * http://www.cs.unibo.it/~renzo/so/pratiche/2013.02.15.pdf
+
  * http://www.cs.unibo.it/~renzo/so/pratiche/2013.02.15.pdf
  * Es.1
+
  * Es.1
  * Eduardo Santarelli
+
  * Eduardo Santarelli
 
  */
 
  */
 
#include <stdio.h>
 
#include <stdio.h>
Line 14: Line 14:
 
#include <stdlib.h>
 
#include <stdlib.h>
  
/* Selector function, called by scandir. Only selects
+
/* Selector function, called by scandir. Only selects
  * palindrome file names. Discards implied directories.
+
  * palindrome file names. Discards implied directories.
 
  */
 
  */
 
int dir_is_pal(const struct dirent* dir){
 
int dir_is_pal(const struct dirent* dir){
 
   int i,j;
 
   int i,j;
  
//discard implied dirs
+
  //discard implied dirs
if(dir->d_name[0]=='.' && strlen(dir->d_name)<3)
+
  if(dir->d_name[0]=='.' && strlen(dir->d_name)<3)
return 0;
+
    return 0;
  
 
   for(i=0, j=strlen(dir->d_name)-1; i<j; i++, j--)
 
   for(i=0, j=strlen(dir->d_name)-1; i<j; i++, j--)
Line 32: Line 32:
  
 
int main(int argc, char** argv){
 
int main(int argc, char** argv){
struct dirent** entry;
+
  struct dirent** entry;
int i, items;
+
  int i, items;
pid_t pid;
+
  pid_t pid;
  
items=scandir("./", &entry, dir_is_pal, alphasort);
+
  items=scandir("./", &entry, dir_is_pal, alphasort);
  
for(i=0; i<items; i++){
+
  for(i=0; i<items; i++){
int status;
+
    int status;
  if((pid=fork())) ; //do nothing
+
    if((pid=fork())) ; //do nothing
  else
+
    else
execl(entry[i]->d_name, entry[i]->d_name, (char*)0);
+
      execl(entry[i]->d_name, entry[i]->d_name, (char*)0);
}
+
  }
  
return 0;
+
  return EXIT_SUCCESS;
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
-Eduardo
 
-Eduardo

Revision as of 03:20, 12 December 2013

Esercizio 1 del 2013.02.15

/*
 *  Prova Pratica di Lab SO 2013.02.15
 *  http://www.cs.unibo.it/~renzo/so/pratiche/2013.02.15.pdf
 *  Es.1
 *  Eduardo Santarelli
 */
#include <stdio.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

/*  Selector function, called by scandir. Only selects
 *  palindrome file names. Discards implied directories.
 */
int dir_is_pal(const struct dirent* dir){
  int i,j;

  //discard implied dirs
  if(dir->d_name[0]=='.' && strlen(dir->d_name)<3)
    return 0;

  for(i=0, j=strlen(dir->d_name)-1; i<j; i++, j--)
    if(dir->d_name[i]!=dir->d_name[j])
      return 0;

  return 1;
}

int main(int argc, char** argv){
  struct dirent** entry;
  int i, items;
  pid_t pid;

  items=scandir("./", &entry, dir_is_pal, alphasort);

  for(i=0; i<items; i++){
    int status;
    if((pid=fork())) ; //do nothing
    else
      execl(entry[i]->d_name, entry[i]->d_name, (char*)0);
  }

  return EXIT_SUCCESS;
}

-Eduardo