read $1 $2 if [ $1 != $2 ] then echo "$1\ $2" else echo $1 fi

Anuncio
Piero Rivera
P1
sin_espacios()
{
read $1 $2
if [ $1 != $2 ]
then
echo "$1\ $2"
else
echo $1
fi
}
find $1 -name *.class *.c | awk '{printf $1 $2}' | sin_espacios | rm -f
printf $1 $2 no preserva espacios, ¿qué pasa si hay más de dos espacios?
Rm no acepta archivos ingresados por entrada estándar, sólo línea de comandos. Por qué sin_espacios compacta nombre
cuando $1 es igual a $2?
10/25
P2
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
static void
sig_usr(int);
void err_sys(char * msg) {
printf("%s \n", msg);
exit(-1);
}
int main(void)
{
if (signal(SIGINT, sig_usr) == SIG_ERR)
err_sys("can't catch SIGINT");
if (signal(SIGTSTP, SIG_IGN) == SIG_ERR)
err_sys("can't ignore SIGTSTP");
for ( ; ; )
pause();
}
static void sig_usr(int signo)
{
if (signo == SIGINT)
{
sleep(2);
printf("received SIGINT\n");
}
else
err_sys("received unexpected signal");
return;
}
/* argument is signal number */
/* No termina después de 2 segundos de llegada de control-C. */
20/25
P3
read ex
while [ $ex != "quit" ];
do
result=`expr $ex`
echo $result
read ex
done
/* Se pedía programa en C
No cumple sintaxis. */
08/25
P4
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
static int count = 0;
static pthread_mutex_t countlock = PTHREAD_MUTEX_INITIALIZER;
int increment(void) {
/* increment the counter */
int error;
if (error = pthread_mutex_lock(&countlock))
return error;
count++;
return pthread_mutex_unlock(&countlock);
}
int decrement(void) {
/* decrement the counter */
int error;
if (error = pthread_mutex_lock(&countlock))
return error;
count--;
return pthread_mutex_unlock(&countlock);
}
static void *incthread(void *arg) { // no calcula máximo
int num;
int i;
char c;
num = *(int *)arg;
fprintf(stderr,"incthread started: %d\n",num);
for (i=0;i<num;i++) {
increment();
}
fprintf(stderr,"incthread done\n");
pthread_exit((void *)2); // cómo interpreta esto?
}
static void *decthread(void *arg) {
int num;
int i;
char c;
num = *(int *)arg;
fprintf(stderr,"decthread started: %d\n",num);
for (i=0;i<num;i++) {
decrement();
}
fprintf(stderr,"decthread done\n");
pthread_exit((void *)2);
}
static void *incthreadNC(void *arg) {
int num;
int i;
char c;
num = *(int *)arg;
fprintf(stderr,"incthread started: %d\n",num);
for (i=0;i<num;i++) {
count++;
}
fprintf(stderr,"incthread done\n");
pthread_exit((void *)2);
}
static void *decthreadNC(void *arg) {
int num;
int i;
char c;
num = *(int *)arg;
fprintf(stderr,"decthread started: %d\n",num);
for (i=0;i<num;i++) {
count--;
}
fprintf(stderr,"decthread done\n");
pthread_exit((void *)2);
}
int main(int argc, char *argv[]) {
pthread_t t0,t1;
int error;
int num = 2000000;
void *tret;
int arg=atoi(argv[1]);
if(arg==1){
fprintf(stderr,"Creating 0\n");
if (error = pthread_create(&t0,NULL,incthread,&num))
fprintf(stderr,"Error creating thread 0: %s\n",strerror(error));
fprintf(stderr,"Creating 1\n");
if (error = pthread_create(&t1,NULL,decthread,&num))
fprintf(stderr,"Error creating thread 1: %s\n",strerror(error));
}else{
if (arg==0) {
fprintf(stderr,"Creating 0\n");
if (error = pthread_create(&t0,NULL,incthreadNC,&num))
fprintf(stderr,"Error creating thread 0: %s\n",strerror(error));
fprintf(stderr,"Creating 1\n");
if (error = pthread_create(&t1,NULL,decthreadNC,&num))
fprintf(stderr,"Error creating thread 1: %s\n",strerror(error));
}else{
printf("El argumento no fue ni 1 ni 0\n",count);
return (-1);
}
}
error = pthread_join(t0, &tret);
if (error != 0)
fprintf(stderr,"can't join with thread 0: %s\n",strerror(error));
error = pthread_join(t1, &tret);
if (error != 0)
fprintf(stderr,"can't join with thread 1: %s\n",strerror(error));
printf("Final count: %d\n",count);
return 0;
}
21/25
Descargar