GATE CSE
First time here? Checkout the FAQ!
x
+9 votes
1.2k views

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.

 

asked in Others by Active (1.8k points)  
edited by | 1.2k views
how can i get benifit from this??
This is the basis- many small tutorials like this will follow to make one ready to do Masters in proper way. Knowing all these makes CS more easier and one more productive.
Sir,I have Kali Linux installed along with windows, can I start practicing from it? Does it works equally good as ubuntu?
@shikar You can definitely use Kali Linux. Coz d commands of Unix r irrespective of its Flavors. By Flavor I meant various types of Unix Based Operating System. It's not just d case with Ubuntu. If u r fine with d basics then you should definitely advance towards higher system level programming. Yeah. :). I worked on ubuntu earlier. D commands and d basic shell scripts don't change though at all. However, there's a difference. As regards d inbuilt shell in different flavors is different. And d type of prompt tells d type of inbuilt shell supported by dat type of Unix Based Operating System. Please go ahead and use it. Encounter any difficulties share it here. It'll be really helpful if individual doubts r discussed. Okay. :)
@ArjunSuresh sir After following the above tutorial for dual booting, are there chances that I may end up loosing my original Windows 10?
@ArjunSuresh After the latest update 17.04 it says it uses a folder rather than a partition for swap space....so should i merge the swap partition to the primary partition or it is still being used in 17.04?

@coder_k chances are low that your windows 10 will be messed, unless you do mistake on your side(like deleting the windows partition by accident etc) but  bootloader may  directly load in ubuntu without showing boot option at start up which can be solved easily...if you are paranoid get the dual booting done by someone who knows what they are doing,meanwhile try bash on windows instead...http://www.windowscentral.com/how-install-bash-shell-command-line-windows-10

oh.. I hadnt noticed that. If you have enough hard disk space leave the swap space as it is as it might be useful in case you need to install a different linux. Otherwise you can merge it.
Any PDF reader for Ubuntu so that I can highlight the text and add comments. ??

5 Answers

+9 votes
Best answer

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

answered by Veteran (287k points)  
selected by
One can also go for latest version of Ubuntu.

Following are some of the good books

1)The Design of d Unix Operating System by Maurice. J.Bach. It covers topics such as File Management, Process Management.

2)For commands and shell scripts:

i)Sumitabh Das(For commands)

ii)Shell Programming Brown Colour book by Yashwant Kanetkar. Very gud for begineers who want to learn basics of shell programming. Handy and Helpful.
+2 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

 

answered by Veteran (48.6k points)  
edited by
Why u divide child process in 3-2,3-2 no. of processes?
+2 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);
}

answered ago by Veteran (48.6k points)  
edited ago by
+1 vote
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.
answered by Boss (6k points)  
0 votes

Second Question:

Download this PDF and print the average score among students.

http://gateoverflow.in/?qa=blob&qa_blobid=3515004380236171380

You may use the "cut" command to filter a column from a file

cut -d',' -f3 a.txt

outputs the third column of the file a.txt each column separated by ",".

awk

is a good command to then find the average. Try it..

answered by Veteran (287k points)  
not getting plz more help ?
you can download the given file. And first extract column 6 to a different file.

In the terminal first do "cd Downloads" because file by default goes to that folder.
Can we share something as answers in this QS post? If I learn something mentionable in this topic, I would like to share.
yes.... you can


Top Users Jun 2017
  1. Bikram

    3968 Points

  2. Arnab Bhadra

    1614 Points

  3. Hemant Parihar

    1502 Points

  4. Niraj Singh 2

    1501 Points

  5. Debashish Deka

    1498 Points

  6. junaid ahmad

    1432 Points

  7. pawan kumarln

    1366 Points

  8. Arjun

    1288 Points

  9. rahul sharma 5

    1258 Points

  10. Rupendra Choudhary

    1242 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 Jun 26 - Jul 02
  1. pawan kumarln

    498 Points

  2. akankshadewangan24

    438 Points

  3. Arjun

    328 Points

  4. Abhisek Das

    326 Points

  5. joshi_nitish

    298 Points


23,447 questions
30,181 answers
67,692 comments
28,521 users