Alejandro Merrello P1 #!/bin/bash #Alejandro Merello cd "$1" rm

Anuncio
Alejandro Merrello
P1
#!/bin/bash
#Alejandro Merello
cd "$1"
rm -f *.o .class / * no ingresa a los subdirectorios */
18/25
P2
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
static void
//perror()
//signal()
//exit()
//fork(), waitpid(), open()
//fork(), write(), dup2(), sleep()
sig_kill(int);
/* one handler for both signals */
int main()
{
int i;
pid_t pid, pidp;
for(i=1;i<__SIGRTMIN;i++) {
if((i!=SIGKILL)&&(i!=SIGSTOP))
if (signal(i,SIG_IGN) == SIG_ERR)
printf("Error\n",i);
}
if (signal(SIGINT, sig_kill) == SIG_ERR)
printf("Error\n");
for ( ; ; )
pause();
return 0;
}
static void sig_kill(int signo)
/* argument is signal number */
{
if (signo == SIGINT)
{
sleep(2);
printf("Llego Ctrl+C\n");
exit(0);
}
return;
}
25/25
P3
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
//fork(), waitpid(), open()
//fork(), execlp(), dup2(), sleep()
//exit()
//fdopen()
//open()
//open()
#include <string.h>
//strtok()
int main()
{
int pfd_w[2],n;
char line[30];
pid_t pid;
FILE *sd;
//if (pipe(pfd_r) < 0) {perror("pipe");exit(1);}
if (pipe(pfd_w) < 0) {perror("pipe");exit(1);}
if ((pid = fork()) < 0) {perror("fork"); exit(1);}
if (pid == 0)
{
int junk= open("/dev/null", O_WRONLY);
dup2(pfd_w[0], 0);
close(pfd_w[1]);
dup2(junk, 2);
execlp("bc","bc",0);
perror("exec");
_exit(127);
}
close(pfd_w[0]);
sd = fdopen(pfd_w[1], "w");
while(1)
{
n=read(0, line, sizeof(line));
fprintf(sd,"%s",line);fflush(sd);
if(strcmp(line,"quit")==0) break;
}
close(pfd_w[1]);
waitpid(pid, NULL, 0);
exit(0);
}
//int system (const char * cmdstring);
/* Faltó line[n]='\0'; luego de leer línea, para finalizar string. Es mejor comparar los 4 primeros caracteres*/
24/25
P4
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define ITERACIONES
2000000
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
long num=0;
void *func(void *args)
{
int i,min=ITERACIONES;
for(i=0;i<ITERACIONES;i++)
{
if(atoi((char *)args[1])==1) pthread_mutex_lock(&mutex);
num--;
if(atoi((char *)args[1])==1) pthread_mutex_unlock(&mutex);
if(num<min)min=num;
}
printf("Valor minimo %d\n",min);
return ((void *)0);
}
int main(argc, char* argv[]) // faltó int argc
{
int i,err,max=0;
pthread_t tidp;
if(pthread_create(&tidp,NULL,func,(void *) argv[])){perror("thread create failed."); exit(1);} // (void *) argv
for(i=0;i<ITERACIONES;i++)
{
if(atoi(argv[1])==1) pthread_mutex_lock(&mutex);
num++;
if(atoi(argv[1])==1) pthread_mutex_unlock(&mutex);
if(num>max)max=num;
}
pthread_join(tidp, NULL);
printf("Valor maximo: %d\n",max); // Faltó mostrar valor final
return 0;
}
24/25
Descargar