edited by
3,051 views
16 votes
16 votes

Hey friends lets learn linux get small exercise and get speed over that platform as it is simlar to other operating system but to get an industrial touch just be familar and get daily small excersie and guidance by Arjun sir ..

. .. follow this post to learn linux --  

you can also post some useful links n discuss here your queries ...

Also make your laptop dual boot and install a linux if you already have not.

edited by

5 Answers

Best answer
11 votes
11 votes

Yes. Especially for those who are going to face IIT interviews or going to join there, these are very beneficial. Advantage of having linux is it provides an easy environment for programming. So lets see how to use it. I'll give some exercices as answers here. You can try them and comment as required..

man cp

the above command shows help for "cp" command. Like this you can do for any command. So, first one- simple.

Copy a file a.txt to b.txt. Then concatenate both and output as c.txt. cat command might be useful. Also google for more interesting options.

<CTRL><ALT><T>

opens a terminal in Ubuntu and you start from there. gedit a.txt opens a file and if you do not have GUI, you can use nano a.txt

selected by
3 votes
3 votes

Task: (example)

  1. Sum of an array of integers using recursive process creation.
  2. Use fork() to create a new process.
  3. Use exit() and wait() to send and receive exit codes from processes. These exit codes will be the subarray sums.
  4. Use two C files, one for actually creating recursive processes and summing all elements and other for launching the executable for the previous file. Use execve() to achieve that.

I am adding just the code for now [ explanation is pending ..adding soon :)  ].

// maincode.c
/*

make clean
make
./run

*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char const *argv[]) {
	char *filenames[] = {"","in"};
	pid_t pid = fork();
	int status;
	if( pid == -1) {
		perror("fork from wrapper failed");
		exit(1);
	}else if( pid > 0) {
		waitpid(pid,&status,0);
		//status = status << 8;
	}else {
		execve("solve",filenames,NULL);
	}

	printf("sum = %d\n",WEXITSTATUS(status));
	// WEXITSTATUS() takes only takes lowest 8 bits
}
// solver.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

#define MIN_SIZE 9

int solve(int *a,int L,int R) {
	if( ( L <= R ) && ((R - L + 1 ) <= MIN_SIZE) ) {
		int res = 0;
		for(int i=L ;i <= R; i++) res += a[i];
		return res;
	}else if (L <= R) {
		int M = (L+R)/2;
		pid_t l_c_pid,r_c_pid;

		l_c_pid = r_c_pid = getpid(); // initialize with some non_zero pid

		l_c_pid = fork();
		
		if( l_c_pid > 0) r_c_pid = fork();

		if(l_c_pid == 0) { // left child 
			int res = solve(a,L,M);
			return res; // goes to the main() function
		}	
		if(r_c_pid == 0) { // right child
			int res = solve(a,M+1,R);
			return res; // goes to the main() function
		}
		if(l_c_pid > 0 && r_c_pid > 0) {
			int res1,res2;
			waitpid(l_c_pid,&res1,0);
			waitpid(r_c_pid,&res2,0);
			//res1 = res1 << 8;
			//res2 = res2 << 8;
			return WEXITSTATUS(res1) + WEXITSTATUS(res2); 
			//printf("HI\n");
			// goes to the main() function
		}
	}
}
int main(int argc, char *argv[]) {
	FILE* file = fopen(argv[1], "r");
	if (file == NULL) {
        printf("Error Reading File\n");
        exit(0);
    }
	int n = 0;
	fscanf(file, "%d", &n);
	int a[n],i = 0;
	while ( fscanf(file, "%d", &n) > 0 ) a[i++] = n;
	fclose(file);
	int res /* all process returning to this point */ = solve(a,0,n-1);
	exit(res);
}


// Makefile -- not necessary 
CC = gcc

CFLAGS  = -std=c99

TARGET1 = run
TARGET2 = solve
W_FILE = solver
M_FILE = maincode

all: $(TARGET1) $(TARGET2) 

$(TARGET1): $(M_FILE).o
	$(CC) $(CFLAGS) -o $(TARGET1) $(M_FILE).o

$(TARGET2): $(W_FILE).o
	$(CC) $(CFLAGS) -o $(TARGET2) $(W_FILE).o

$(W_FILE).o: $(W_FILE).c
	$(CC) $(CFLAGS) -c $(W_FILE).c -o $(W_FILE).o

$(M_FILE).o: $(M_FILE).c
	$(CC) $(CFLAGS) -c $(M_FILE).c -o $(M_FILE).o

clean:
	$(RM) *.o $(TARGET1) $(TARGET2)
// in
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
edited by
3 votes
3 votes

Usage of IPC semaphore utilities ( IPC - inter process communication) and shared memory.

Example : producer consumer problem - version 1. Infinite production and consumption with FINITE buffer size.

In this example there are three C programs

  1. prog.c - Initial program to invoke procucer and consumer programs ( or processes ).
  2. prod.c - producer program
  3. cons.c - consumer program

sem_utility used for semaphore operations functions.

resources.h is used for shared memory and some other usage.

// resources.h
#define SIZE 1024
#define BUFSIZE     10
#define N_SEM       3
#define MUTEX       0
#define FULL        1
#define EMPTY       2
struct shared_mem {
	int buffer[SIZE];
	int in,out;
};
// sem_utility.h
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <sys/shm.h>
union semun {
    int val;                    
    struct semid_ds *buf;       
    unsigned short int *array;  
    struct seminfo *__buf;      
};

static int set_semvalue(int,int);
static void del_semvalue(void);
static int semaphore_p(int);
static int semaphore_v(int);
static int sem_id;

static int set_semvalue(int sem_no,int ini_val) {
    union semun sem_union;
    sem_union.val = ini_val;
    if (semctl(sem_id, sem_no, SETVAL, sem_union) == -1) return(0);
    return(1);
}
static void del_semvalue(void) {
    union semun sem_union;
    if (semctl(sem_id, 0, IPC_RMID, sem_union) == -1)
        fprintf(stderr, "Failed to delete semaphore\n");
}
static int semaphore_p(int sem_no) {
    struct sembuf sem_b;
    sem_b.sem_num = sem_no;
    sem_b.sem_op = -1;
    sem_b.sem_flg = SEM_UNDO;
    if(semop(sem_id, &sem_b, 1) == -1) {
        fprintf(stderr, "semaphore_p failed\n");
        return(0);
    }
    //printf("%s\n", );
    //printf("dropped\n");
    return(1);
}
static int semaphore_v(int sem_no) {
    struct sembuf sem_b;
    sem_b.sem_num = sem_no;
    sem_b.sem_op = 1;
    sem_b.sem_flg = SEM_UNDO;
    if (semop(sem_id, &sem_b, 1) == -1) {
        fprintf(stderr, "semaphore_v failed\n");
        return(0);
    }
    return(1);
}


// prog.c
#include "sem_utility.h"
#include "resources.h"
int sem_ini_val[N_SEM] = {1,0,BUFSIZE};
int main(int argc, char *argv[]) {
    int sem_number;
    char * cc[] = {NULL};
    srand(time(NULL));
    void *shared_memory = (void *)0;
    struct shared_mem *ptr;
    int shm_id;
    shm_id = shmget((key_t)1111,sizeof(struct shared_mem),0666|IPC_CREAT);
    if(shm_id == -1) {
        fprintf(stderr, "shmget failed\n");
        exit(EXIT_FAILURE);
    }
    shared_memory = shmat(shm_id,(void*)0,0);
    if(shared_memory == (void*)-1) {
        fprintf(stderr, "shmat failed\n");
        exit(EXIT_FAILURE);
    }
    ptr = (struct shared_mem *)shared_memory;
    ptr->in = 0;
    ptr->out = 0;
    sem_id = semget((key_t)3333, N_SEM, 0666 | IPC_CREAT);
    for(sem_number=0;sem_number<N_SEM;sem_number++)
        set_semvalue(sem_number,sem_ini_val[sem_number]);
    pid_t producer_pid, consumer_pid;
    consumer_pid = producer_pid = getpid();
    consumer_pid = fork();
    if(consumer_pid > 0) producer_pid = fork();
    if(producer_pid == 0) { 
        execvp("producer",cc);
    }
    if(consumer_pid == 0) execvp("consumer",cc);
    if(consumer_pid > 0 && producer_pid > 0) {
        int res1,res2;
        waitpid(consumer_pid,&res1,0);
        waitpid(producer_pid,&res2,0);
    }
    sleep(2);
    del_semvalue();
    if(shmdt(shared_memory) == -1) {
        fprintf(stderr, "shmdt failed\n");
        exit(EXIT_FAILURE);
    }
    if(shmctl(shm_id,IPC_RMID,0) == -1) {
        fprintf(stderr, "shmctl failed\n");
        exit(EXIT_FAILURE);
    }
    printf("main programm exited\n");
    exit(EXIT_SUCCESS);
}
// cons.c
#include "sem_utility.h"
#include "resources.h"
int main(int argc, char *argv[]) {
    printf("I am consumer\n");
    srand(time(NULL));
    void *shared_memory = (void *)0;
    struct shared_mem *ptr;
    int shm_id;
    shm_id = shmget((key_t)1111,sizeof(struct shared_mem),0666|IPC_CREAT);
    if(shm_id == -1) {
        fprintf(stderr, "shmget failed\n");
        exit(EXIT_FAILURE);
    }
    shared_memory = shmat(shm_id,(void*)0,0);
    if(shared_memory == (void*)-1) {
        fprintf(stderr, "shmat failed\n");
        exit(EXIT_FAILURE);
    }
    sem_id = semget((key_t)3333, 1, 0666 | IPC_CREAT);
    ptr = (struct shared_mem *)shared_memory;
    while(1) {
        int pause_time = rand()%3;
        sleep(pause_time);

        semaphore_p(FULL);
        semaphore_p(MUTEX);

        int data = ptr->buffer[ptr->out];
        ptr->out = (ptr->out + 1) % BUFSIZE;
        printf("Consumed: %d    out = %d\n", data,ptr->out);

        semaphore_v(MUTEX);
        semaphore_v(EMPTY);
    }
    if(shmdt(shared_memory) == -1) {
        fprintf(stderr, "shmdt failed\n");
        exit(EXIT_FAILURE);
    } 
    printf("consumer exited\n");
    exit(EXIT_SUCCESS); 
}
// prod.c
#include "sem_utility.h"
#include "resources.h"
int main(int argc, char *argv[]) {
    printf("I am producer\n");
    srand(time(NULL));
    void *shared_memory = (void *)0;
    struct shared_mem *ptr;
    int shm_id;
    shm_id = shmget((key_t)1111,sizeof(struct shared_mem),0666|IPC_CREAT);
    if(shm_id == -1) {
        fprintf(stderr, "shmget failed\n");
        exit(EXIT_FAILURE);
    }
    shared_memory = shmat(shm_id,(void*)0,0);
    if(shared_memory == (void*)-1) {
        fprintf(stderr, "shmat failed\n");
        exit(EXIT_FAILURE);
    }
    sem_id = semget((key_t)3333, 1, 0666 | IPC_CREAT);
    ptr = (struct shared_mem *)shared_memory;
    int i = 20;
    while(1) {
        int pause_time = rand()%3;
        sleep(pause_time);
        
        semaphore_p(EMPTY);
        semaphore_p(MUTEX);
        
        int data = rand()%1000;
        ptr->buffer[ptr->in] = data;
        ptr->in = (ptr->in + 1) % BUFSIZE;
        printf("Produced: %d    in = %d\n", data,ptr->in);
        
     
        semaphore_v(MUTEX);
        semaphore_v(FULL);

    }
    if(shmdt(shared_memory) == -1) {
        fprintf(stderr, "shmdt failed\n");
        exit(EXIT_FAILURE);
    }
    printf("producer exited\n");
    exit(EXIT_SUCCESS);
}

edited by
1 votes
1 votes
Basically Unix was started as a project knows as MULTUCS in AT&T Bell Labs. C Prog Language was developed in Unix alone.

It advisable to first cover d commands. By using man <command name> one can get d inbuilt help of command from d OS itself.

You can type dis at the command prompt.

The type of prompt dat is present defines d type of shell dat d version/flavours of Unix supports as many of dem r available. And most of them r open source as well.

Shell scripts can be typed using gedit which is d GUI editor or d inbuilt editor of d system. It has d main user which is known as super admin, while other users r group users and indivdual ones.

One of d basic feature of Unix is dat it is secure. The security comes abt thru d various permissions dat r given to d various category of users. And to top it all the tree kind of File System tat it carries too. :).

More on d same is given in d books mentioned above.

Related questions

0 votes
0 votes
0 answers
2
makhdoom ghaya asked Aug 30, 2016
692 views
Which command allows you to view your file $24$ lines at a time ?MoreCatPgNone of the above
3 votes
3 votes
1 answer
3
go_editor asked Jul 22, 2016
4,154 views
Which of the following is not a part of an expert system shell?Knowledge baseInterference engineExplanation facilityNone of the above
0 votes
0 votes
2 answers
4
Pooja Khatri asked Jul 13, 2018
1,455 views
Which UNIX/Linux command is used to make all files and sub-directions in the directory "progs" executable by all users?chmod - R a + x progschmod - R 222 progschmod - X a...