You are on page 1of 7

Submitted by:

Md. Rashedul Islam


1511108042
Sec:3
Parameter passing and threading in C
Parameter passing:
When a function gets executed in the program, the execution control is
transferred from calling-function to called function and executes
function definition, and finally comes back to the calling function. When
the execution control is transferred from calling-function to called-
function it may carry one or number of data values. These data values
are called as parameters.

In C, there are two types of parameters and they are as follows...

 Actual Parameters
 Formal Parameters

The actual parameters are the parameters that are speficified in calling
function. The formal parameters are the parameters that are declared at
called function. When a function gets executed, the copy of actual
parameter values are copied into formal parameters.

In C Programming Language, there are two methods to pass parameters


from calling function to called function and they are as follows...

 Call by Value
 Call by Reference

Call by value:
This method uses in-mode semantics. Changes made to formal
parameter do not get transmitted back to the caller. Any modifications to
the formal parameter variable inside the called function or method affect
only the separate storage location and will not be reflected in the actual
parameter in the calling environment. This method is also called as call
by value.

#include <stdio.h>
  
void func(int a, int b)
{
    a += b;
    printf("In func, a = %d b = %d\n", a, b);
}
int main(void)
{
    int x = 5, y = 7;
  
    // Passing parameters
    func(x, y);
    printf("In main, x = %d y = %d\n", x, y);
    return 0;
}

Output:
In func, a = 12 b = 7
In main, x = 5 y = 7

Call by reference:

This technique uses in/out-mode semantics. Changes made to formal


parameter do get transmitted back to the caller through parameter
passing. Any changes to the formal parameter are reflected in the actual
parameter in the calling environment as formal parameter receives a
reference (or pointer) to the actual data. This method is also called as
<em>call by reference. This method is efficient in both time and space.

#include <stdio.h>
  
void swapnum(int* i, int* j)
{
    int temp = *i;
    *i = *j;
    *j = temp;
}
  
int main(void)
{
    int a = 10, b = 20;
  
    // passing parameters
    swapnum(&a, &b);
  
    printf("a is %d and b is %d\n", a, b);
    return 0;
}

Output:

a is 20 and b is 10

Other methods of Parameter Passing

These techniques are older and were used in earlier programming


languages like Pascal, Algol and Fortran. These techniques are not
applicable in high level languages.

1. Pass by Result : This method uses out-mode semantics. Just


before control is transfered back to the caller, the value of the
formal parameter is transmitted back to the actual parameter.T his
method is sometimes called call by result. In general, pass by result
technique is implemented by copy.
2. Pass by Value-Result : This method uses in/out-mode semantics.
It is a combination of Pass-by-Value and Pass-by-result. Just
before the control is transferred back to the caller, the value of the
formal parameter is transmitted back to the actual parameter. This
method is sometimes called as call by value-result
3. Pass by name : This technique is used in programming language
such as Algol. In this technique, symbolic “name” of a variable is
passed, which allows it both to be accessed and update.

Threading:

What is a Thread?
A thread is a single sequence stream within in a process. Because
threads have some of the properties of processes, they are sometimes
called lightweight processes.

What are the differences between process and thread?


Threads are not independent of one other like processes as a result
threads shares with other threads their code section, data section and OS
resources like open files and signals. But, like process, a thread has its
own program counter (PC), a register set, and a stack space.

Why Multithreading?
Threads are popular way to improve application through parallelism. For
example, in a browser, multiple tabs can be different threads. MS word
uses multiple threads, one thread to format the text, other thread to
process inputs, etc.
Threads operate faster than processes due to following reasons:
1) Thread creation is much faster.
2) Context switching between threads is much faster.
3) Threads can be terminated easily
4) Communication between threads is faster.

A simpe example:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>  //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>
  
// A normal C function that is executed as a thread 
// when its name is specified in pthread_create()
void *myThreadFun(void *vargp)
{
    sleep(1);
    printf("Printing GeeksQuiz from Thread \n");
    return NULL;
}
   
int main()
{
    pthread_t thread_id;
    printf("Before Thread\n");
    pthread_create(&thread_id, NULL, myThreadFun, NULL);
    pthread_join(thread_id, NULL);
    printf("After Thread\n");
    exit(0);
}
In main() we declare a variable called thread_id, which is of type
pthread_t, which is an integer used to identify the thread in the system.
After declaring thread_id, we call pthread_create() function to create a
thread.
pthread_create() takes 4 arguments.
The first argument is a pointer to thread_id which is set by this function.
The second argument specifies attributes. If the value is NULL, then
default attributes shall be used.
The third argument is name of function to be executed for the thread to
be created.
The fourth argument is used to pass arguments to the function,
myThreadFun.
The pthread_join() function for threads is the equivalent of wait() for
processes. A call to pthread_join blocks the calling thread until the
thread with identifier equal to the first argument terminates.

Advantages of Threads over Multiple Processes

 Context Switching    Threads are very inexpensive to create and


destroy, and they are inexpensive to represent. For example, they
require space to store, the PC, the SP, and the general-purpose
registers, but they do not require space to share memory
information, Information about open files of I/O devices in use,
etc. With so little context, it is much faster to switch between
threads. In other words, it is relatively easier for a context switch
using threads.
 Sharing    Treads allow the sharing of a lot resources that cannot
be shared in process, for example, sharing code section, data
section, Operating System resources like open file etc.

Disadvantages of Threads over Multiprocesses

 Blocking     The major disadvantage if that if the kernel is single


threaded, a system call of one thread will block the whole process
and CPU may be idle during the blocking period.
 Security    Since there is, an extensive sharing among threads there
is a potential problem of security. It is quite possible that one
thread over writes the stack of another thread (or damaged shared
data) although it is very unlikely since threads are meant to
cooperate on a single task.
References:

https://www.cs.utah.edu/~germain/PPS/Topics/C_Language/c_functions
.html

https://www.geeksforgeeks.org/parameter-passing-techniques-in-c-cpp/

http://www.csc.villanova.edu/~mdamian/threads/posixthreads.html

https://www.personal.kent.edu/
%7Ermuhamma/OpSystems/Myos/threads.htm

http://www.btechsmartclass.com/c_programming/C-Parameter-
Passing.html
https://www.geeksforgeeks.org/multithreading-c-2/

You might also like