Rodrigo Mujica P1 #!/bin/bash rm -f $1*.class rm

Anuncio
Rodrigo Mujica
P1
#!/bin/bash
rm -f $1*.class
rm -f $1*.o
/* Esto no permite baja por los subdirectorios */
15/25
P2
#include <signal.h>
#include <stdio.h>
#include <stdlib.h> /* required by exit() */
static void sig_usr(int num){
if (num == SIGINT){
printf("Llego Control-C\n");
exit(0);
}
else
printf("Llego señal erronea, para terminar aprete Ctrl-C");
return;
}
int main(void)
{
if (signal(SIGINT, sig_usr) == SIG_ERR)
printf("Error al capturar SIGINT");
if (signal(SIGUSR1, SIG_IGN) == SIG_ERR)
printf("Error al capturar SIGINT");
if (signal(SIGUSR2, SIG_IGN) == SIG_ERR)
printf("Error al capturar SIGINT");
for ( ; ; )
pause();
}
25/25
P3
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
int main(int argc, char *argv[])
{
pid_t pid;
int status;
char aux[100],sol[100];
int pfd[2],post[2];
FILE * sd;
FILE * pd;
printf("ingreso a bc\n");
/*
* Create a pipe.
*/
if (pipe(pfd) < 0) {
perror("pipe");
exit(1);
}
if (pipe(post) < 0) {
perror("pipe");
exit(1);
}
printf("creo pipas\n");
/*
* Create a child process.
*/
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}
if (pid == 0) {
int junk;
/*
* Attach standard input of this child process to read from the pipe.
*/
dup2(pfd[0], 0);
dup2(post[1], 1);
junk = open("/dev/null", O_WRONLY);
dup2(junk, 1); /* throw away any message sent to screen*/ /* Cómo conocerá la respuesta */
dup2(junk, 2); /* throw away any error message */
//execlp("octave", "octave", "-i", NULL); /* Esto corresponde a otro problema */
execlp("octve","octave",NULL);
perror("exec");
exit(-1);
}
printf("creo hijo, seteando pipas\n");
//dup2(pfd[1],0);
sd = fdopen(pfd[1], "w");
pd = fdopen(post[0], "r");
scanf("%s",&aux); /* aux es un arreglo no lleva & */
printf("%s\n",aux);
fprintf(sd, "%s\n",aux);
fscanf(pd,&sol);
printf("%s\n",sol); /* idem */
//dup2(post[0],1);
/*
* Wait for the child to exit and
* close de pipe.
*/
waitpid(pid, &status, 0);
fclose(sd);
/*
* Exit with a status of 0, indicating that
* everything went fine.
*/
exit(0);
}
10/25
P4
#include <pthread.h>
static int num = 0;
static pthread_mutex_t numlock = PTHREAD_MUTEX_INITIALIZER;
void *ret1, *ret2;
pthread_t
tid1, tid2;
int massec(void) {
// i, j no definidos
int error,max=0;
for(i=1;i<=2000;i++){
for(j=1;j<=1000;j++){
if (error = pthread_mutex_lock(&numlock))
return error;
num++;
if(num>max) max=num;
pthread_mutex_unlock(&numlock);
}
}
return(max); // hebras retornan punteros
}
int menossec(void) {
int error,min=0;
for(i=1;i<=2000;i++){
for(j=1;j<=1000;j++){
if (error = pthread_mutex_lock(&numlock))
return error;
num--;
if(num<min) min=num;
pthread_mutex_unlock(&numlock);
}
}
err = pthread_join(tid1, &ret1);
if (err != 0)
err_quit("can't join with thread 1: %s\n", strerror(err));
printf("MAX = %d\nActual = %d\n", ret1,num);
}
int mas(void) {
int i,j,max=0;
for(i=1;i<=2000;i++){
for(j=1;j<=1000;j++){
num++;
if(num>max) max=num;
}
}
return(max);
}
int menos(void) {
int i,j,min=0;
for(i=1;i<=2000;i++){
for(j=1;j<=1000;j++){
num--;
if(num<min) min=num;
}
}
err = pthread_join(tid1, &ret1);
if (err != 0)
err_quit("can't join with thread 1: %s\n", strerror(err));
printf("MAX = %d\nActual = %d\n", (int)ret1,num);
return(min);
}
int main(int argc,char argv[]) // falta * argv[]
{
int
err;
if(argv[1]=='1'){ // compara string con char
err = pthread_create(&tid1, NULL, massec, NULL);
if (err != 0)
err_quit("can't create massec: %s\n", strerror(err));
err = pthread_create(&tid2, NULL, menossec, NULL);
if (err != 0)
err_quit("can't create menossec: %s\n", strerror(err));
}
else if (argv[1]=='0'){
err = pthread_create(&tid1, NULL, mas, NULL);
if (err != 0)
err_quit("can't create massec: %s\n", strerror(err));
err = pthread_create(&tid2, NULL, menos, NULL);
if (err != 0)
err_quit("can't create menossec: %s\n", strerror(err));
}
err = pthread_join(tid2, &ret2);
if (err != 0)
err_quit("can't join with thread 2: %s\n", strerror(err));
printf("MIN %d\n", (int)ret2);
exit(0);
}
20/25
Descargar