<commit_before>#include <stdio.h>
#include <stdlib.h>
#include "thread.h"

typedef struct semaphore {
	int value;                           /*semaphore structure having value and semaphore queue */
	TCB_t  *semQ;
} Semaphore_t;

Semaphore_t * CreateSem(Semaphore_t **sem, int  num)       /* creating the semaphore and initilaizing it's value */
{
	(*sem) = (Semaphore_t *) malloc(sizeof(Semaphore_t));
	(*sem)->value = num;
	(*sem)->semQ = NULL;

	return *sem;
}


/**
 * Pre-Condition: runQ shouldn't be NULL
 * @param sem semaphore
 */
void P(Semaphore_t **sem)               /* p operation of semaphore */
{

	(*sem)->value = (*sem)->value - 1;  /* decrementing the value */

	if ((*sem)->value < 0)           /* if less than 0 blocking the current process on sempahore queue */
	{
		printf("Shouldnt come here\n");
		TCB_t *temp;
		temp = DelQ(&runQ, runQ);
		if ((*sem)->semQ == NULL)                         /* if the item is the 1st node then queue pointer should point the item */
		{
			InitQ(temp);
			(*sem)->semQ = temp;
		}
		else
		{
			AddQ((*sem)->semQ, temp);
		}

		ucontext_t from, to;
		getcontext(&to);					// main
		// runQ->context = from;
		from = temp->context;					// function1
		swapcontext(&from, &to);
	}


}




void V(Semaphore_t **sem)
{
	(*sem)->value = (*sem)->value + 1;   /* incrementing the value of sempahore */

	if ((*sem)->value <= 0)
	{
		TCB_t *temp;               /* deleting the element from semaphore q and putiing it on runQ */
		temp = DelQ(&runQ, (*sem)->semQ);
		AddQ(runQ, temp);
	}
	// yield();
}
<commit_msg>[Bug] Swap Context in P() written reverse!<commit_after>#include <stdio.h>
#include <stdlib.h>
#include "thread.h"

typedef struct semaphore {
	int value;                           /*semaphore structure having value and semaphore queue */
	TCB_t  *semQ;
} Semaphore_t;

Semaphore_t * CreateSem(Semaphore_t **sem, int  num)       /* creating the semaphore and initilaizing it's value */
{
	(*sem) = (Semaphore_t *) malloc(sizeof(Semaphore_t));
	(*sem)->value = num;
	(*sem)->semQ = NULL;

	return *sem;
}


/**
 * Pre-Condition: runQ shouldn't be NULL
 * @param sem semaphore
 */
void P(Semaphore_t **sem)               /* p operation of semaphore */
{

	(*sem)->value = (*sem)->value - 1;  /* decrementing the value */

	if ((*sem)->value < 0)           /* if less than 0 blocking the current process on sempahore queue */
	{
		TCB_t *temp;
		temp = DelQ(&runQ, runQ);
		if ((*sem)->semQ == NULL) /* if the item is the 1st node then queue pointer should point the item */
		{
			InitQ(temp);
			(*sem)->semQ = temp;
		}
		else
		{
			AddQ((*sem)->semQ, temp);
		}

		ucontext_t from, to;
		getcontext(&from);					// main
		to = temp->context;					// function1
		swapcontext(&from, &to);
	}


}




void V(Semaphore_t **sem)
{
	(*sem)->value = (*sem)->value + 1;   /* incrementing the value of sempahore */

	if ((*sem)->value <= 0)
	{
		TCB_t *temp;               /* deleting the element from semaphore q and putiing it on runQ */
		temp = DelQ(&runQ, (*sem)->semQ);
		AddQ(runQ, temp);
	}
	// yield();
}

{'fraction_non_alphanumeric': 0.1634050880626223, 'fraction_numerical': 0.0045662100456621, 'mean_word_length': 2.6338862559241707, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2411710', 'n_tokens_mistral': 1212, 'n_tokens_neox': 1104, 'n_words': 307}